parser

package
v0.0.0-...-44df2ee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const HTMLTitleParserName = "html"

HTMLTitleParserName name of HTMLTitleParser for the parser factory

View Source
const MarkdownTitleParserName = "md"

MarkdownTitleParserName name of MarkdownTitleParser for the parser factory

View Source
const MultiParserName = "multi"

Variables

This section is empty.

Functions

func SetInnerParser

func SetInnerParser(name string, creator Creator)

SetInnerParser 注册新的标题解析器

Types

type ContentEncoder

type ContentEncoder func(string) string

ContentEncoder 对URL进行编码的函数,传入URL,传出编码后的结果

type Creator

type Creator func(string) TitleParser

Creator 创建parser的函数类型,接受一个string作为顶层标题结构,被SetParser调用

type DefaultFilter

type DefaultFilter struct {
	// 排除某些标题
	ExcludeTitles []string
	// 排除符合regexp匹配的title
	ExcludeRegExp *regexp.Regexp
}

func (*DefaultFilter) FilterTitleContent

func (filter *DefaultFilter) FilterTitleContent(content string) bool

FilterTitleContent 根据title的内容过滤标题 返回true表示title需要被过滤

func (*DefaultFilter) SetExcludeRegExp

func (filter *DefaultFilter) SetExcludeRegExp(reg string) (err error)

func (*DefaultFilter) SetExcludeTitles

func (filter *DefaultFilter) SetExcludeTitles(titles string)

SetExcludeTitles 将由逗号分隔的一串标题分割成slice并设置

type Option

type Option func(*Parser)

func WithFilter

func WithFilter(filter TitleFilter) Option

WithFilter 根据标题内容确定是否需要被解析

func WithScanType

func WithScanType(t string) Option

WithScanType 设置需要解析的标题类型(html, markdown)

func WithTOCMark

func WithTOCMark(mark string) Option

TocMark 设置文档中的toc标记

func WithTopTag

func WithTopTag(tag string) Option

WithTopTag 设置开始解析的标题层级,值为h1-h5

func WithURLEncoder

func WithURLEncoder(encoder ContentEncoder) Option

WithURLEncoder 设置标题内容的编码器,非ascii字符作为URL时可能需要编码

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser 进行整个markdown的解析

func GetParser

func GetParser(options ...Option) *Parser

GetParser 根据name创建markdown文档解析器

func (*Parser) Parse

func (p *Parser) Parse(file io.Reader) []*TitleNode

Parse 根据topTag和scanType解析文件中的标题行 默认从tocMark之后的位置开始解析,如果没有找到tocMark则解析整个markdown文件

type TitleFilter

type TitleFilter interface {
	FilterTitleContent(content string) bool
}

TitleFilter 根据规则过滤标题

type TitleNode

type TitleNode struct {
	// contains filtered or unexported fields
}

TitleNode 标题节点,按顶层标签构建的节点树 节点树的生长方向是单一的固定的, 假设节点按从左向右生长则最新添加的子节点一定在最右或者是最右子节点的后代

func NewTitleNode

func NewTitleNode(tag, id, content string) *TitleNode

NewTitleNode 创建新节点,tag是html tag名称

func (*TitleNode) AddChild

func (t *TitleNode) AddChild(child *TitleNode)

AddChild 添加子节点 如果不是直接后代则作为最后一个直接子节点的孩子被添加

func (*TitleNode) HTML

func (t *TitleNode) HTML() string

HTML 生成当前节点及其子节点的html

func (*TitleNode) IsChildNode

func (t *TitleNode) IsChildNode(node *TitleNode) bool

IsChildNode 检查node是否是当前节点的子节点 按tagName比较:h1 > h2 > ... > h5 因为markdown被顺序解析,所以tag更小意味着它是当前节点的子节点

func (*TitleNode) Markdown

func (t *TitleNode) Markdown(indent string, depth int) string

Markdown 生成当前节点及其子节点的markdown depth为0表示当前节点没有父节点,因此不设置缩进,只设置其子节点的缩进

type TitleParser

type TitleParser interface {
	// content如果不是标题就返回nil,否则返回TitleNode
	Parse(content string) *TitleNode
}

TitleParser 解析文件中的标题结构

func GetInnerParser

func GetInnerParser(name, topTag string) (TitleParser, error)

GetInnerParser 获取标题解析器。topTag是可标题的最大级别(例如h1-h5中的h1)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL