Source File
html_block.go
Belonging Package
github.com/yuin/goldmark/parser
package parser
import (
)
var allowedBlockTags = map[string]bool{
"address": true,
"article": true,
"aside": true,
"base": true,
"basefont": true,
"blockquote": true,
"body": true,
"caption": true,
"center": true,
"col": true,
"colgroup": true,
"dd": true,
"details": true,
"dialog": true,
"dir": true,
"div": true,
"dl": true,
"dt": true,
"fieldset": true,
"figcaption": true,
"figure": true,
"footer": true,
"form": true,
"frame": true,
"frameset": true,
"h1": true,
"h2": true,
"h3": true,
"h4": true,
"h5": true,
"h6": true,
"head": true,
"header": true,
"hr": true,
"html": true,
"iframe": true,
"legend": true,
"li": true,
"link": true,
"main": true,
"menu": true,
"menuitem": true,
"meta": true,
"nav": true,
"noframes": true,
"ol": true,
"optgroup": true,
"option": true,
"p": true,
"param": true,
"section": true,
"source": true,
"summary": true,
"table": true,
"tbody": true,
"td": true,
"tfoot": true,
"th": true,
"thead": true,
"title": true,
"tr": true,
"track": true,
"ul": true,
}
var htmlBlockType1OpenRegexp = regexp.MustCompile(`(?i)^[ ]{0,3}<(script|pre|style)(?:\s.*|>.*|/>.*|)\n?$`)
var htmlBlockType1CloseRegexp = regexp.MustCompile(`(?i)^.*</(?:script|pre|style)>.*`)
var htmlBlockType2OpenRegexp = regexp.MustCompile(`^[ ]{0,3}<!\-\-`)
var htmlBlockType2Close = []byte{'-', '-', '>'}
var htmlBlockType3OpenRegexp = regexp.MustCompile(`^[ ]{0,3}<\?`)
var htmlBlockType3Close = []byte{'?', '>'}
var htmlBlockType4OpenRegexp = regexp.MustCompile(`^[ ]{0,3}<![A-Z]+.*\n?$`)
var htmlBlockType4Close = []byte{'>'}
var htmlBlockType5OpenRegexp = regexp.MustCompile(`^[ ]{0,3}<\!\[CDATA\[`)
var htmlBlockType5Close = []byte{']', ']', '>'}
var htmlBlockType6Regexp = regexp.MustCompile(`^[ ]{0,3}</?([a-zA-Z0-9]+)(?:\s.*|>.*|/>.*|)\n?$`)
var htmlBlockType7Regexp = regexp.MustCompile(`^[ ]{0,3}<(/)?([a-zA-Z0-9\-]+)(` + attributePattern + `*)(:?>|/>)\s*\n?$`)
type htmlBlockParser struct {
}
var defaultHTMLBlockParser = &htmlBlockParser{}
func () BlockParser {
return defaultHTMLBlockParser
}
func ( *htmlBlockParser) () []byte {
return []byte{'<'}
}
func ( *htmlBlockParser) ( ast.Node, text.Reader, Context) (ast.Node, State) {
var *ast.HTMLBlock
, := .PeekLine()
:= .LastOpenedBlock().Node
if := .BlockOffset(); < 0 || [] != '<' {
return nil, NoChildren
}
if := htmlBlockType1OpenRegexp.FindSubmatchIndex(); != nil {
= ast.NewHTMLBlock(ast.HTMLBlockType1)
} else if htmlBlockType2OpenRegexp.Match() {
= ast.NewHTMLBlock(ast.HTMLBlockType2)
} else if htmlBlockType3OpenRegexp.Match() {
= ast.NewHTMLBlock(ast.HTMLBlockType3)
} else if htmlBlockType4OpenRegexp.Match() {
= ast.NewHTMLBlock(ast.HTMLBlockType4)
} else if htmlBlockType5OpenRegexp.Match() {
= ast.NewHTMLBlock(ast.HTMLBlockType5)
} else if := htmlBlockType7Regexp.FindSubmatchIndex(); != nil {
:= [2] > -1 && bytes.Equal([[2]:[3]], []byte("/"))
:= [6] != [7]
:= strings.ToLower(string([[4]:[5]]))
, := allowedBlockTags[]
if {
= ast.NewHTMLBlock(ast.HTMLBlockType6)
} else if != "script" && != "style" && != "pre" && !ast.IsParagraph() && !( && ) { // type 7 can not interrupt paragraph
= ast.NewHTMLBlock(ast.HTMLBlockType7)
}
}
if == nil {
if := htmlBlockType6Regexp.FindSubmatchIndex(); != nil {
:= string([[2]:[3]])
, := allowedBlockTags[strings.ToLower()]
if {
= ast.NewHTMLBlock(ast.HTMLBlockType6)
}
}
}
if != nil {
.Advance(.Len() - 1)
.Lines().Append()
return , NoChildren
}
return nil, NoChildren
}
func ( *htmlBlockParser) ( ast.Node, text.Reader, Context) State {
:= .(*ast.HTMLBlock)
:= .Lines()
, := .PeekLine()
var []byte
switch .HTMLBlockType {
case ast.HTMLBlockType1:
if .Len() == 1 {
:= .At(0)
if htmlBlockType1CloseRegexp.Match(.Value(.Source())) {
return Close
}
}
if htmlBlockType1CloseRegexp.Match() {
.ClosureLine =
.Advance(.Len() - 1)
return Close
}
case ast.HTMLBlockType2:
= htmlBlockType2Close
fallthrough
case ast.HTMLBlockType3:
if == nil {
= htmlBlockType3Close
}
fallthrough
case ast.HTMLBlockType4:
if == nil {
= htmlBlockType4Close
}
fallthrough
case ast.HTMLBlockType5:
if == nil {
= htmlBlockType5Close
}
if .Len() == 1 {
:= .At(0)
if bytes.Contains(.Value(.Source()), ) {
return Close
}
}
if bytes.Contains(, ) {
.ClosureLine =
.Advance(.Len() - 1)
return Close
}
case ast.HTMLBlockType6, ast.HTMLBlockType7:
if util.IsBlank() {
return Close
}
}
.Lines().Append()
.Advance(.Len() - 1)
return Continue | NoChildren
}
}
func ( *htmlBlockParser) () bool {
return true
}
func ( *htmlBlockParser) () bool {
return false
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |