package parser import ( "bytes" "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/text" "github.com/yuin/goldmark/util" "regexp" ) type rawHTMLParser struct { HTMLConfig } // NewRawHTMLParser return a new InlineParser that can parse // inline htmls func NewRawHTMLParser(opts ...HTMLOption) InlineParser { p := &rawHTMLParser{} for _, o := range opts { o.SetHTMLOption(&p.HTMLConfig) } return p } func (s *rawHTMLParser) Trigger() []byte { return []byte{'<'} } func (s *rawHTMLParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.Node { line, _ := block.PeekLine() if len(line) > 1 && util.IsAlphaNumeric(line[1]) { return s.parseMultiLineRegexp(openTagRegexp, block, pc) } if len(line) > 2 && line[1] == '/' && util.IsAlphaNumeric(line[2]) { return s.parseMultiLineRegexp(closeTagRegexp, block, pc) } if bytes.HasPrefix(line, []byte("|`) var processingInstructionRegexp = regexp.MustCompile(`^(?:<\?).*?(?:\?>)`) var declRegexp = regexp.MustCompile(`^]*>`) var cdataRegexp = regexp.MustCompile(``) func (s *rawHTMLParser) parseSingleLineRegexp(reg *regexp.Regexp, block text.Reader, pc Context) ast.Node { line, segment := block.PeekLine() match := reg.FindSubmatchIndex(line) if match == nil { return nil } node := ast.NewRawHTML() node.AppendChild(node, ast.NewRawTextSegment(segment.WithStop(segment.Start+match[1]))) block.Advance(match[1]) return node } var dummyMatch = [][]byte{} func (s *rawHTMLParser) parseMultiLineRegexp(reg *regexp.Regexp, block text.Reader, pc Context) ast.Node { sline, ssegment := block.Position() var m [][]byte if s.FilterTags != nil { m = block.FindSubMatch(reg) } else { if block.Match(reg) { m = dummyMatch } } if m != nil { if s.FilterTags != nil { tagName := string(m[1]) if _, ok := s.FilterTags[tagName]; ok { return nil } } node := ast.NewRawHTML() eline, esegment := block.Position() block.SetPosition(sline, ssegment) for { line, segment := block.PeekLine() if line == nil { break } l, _ := block.Position() start := segment.Start if l == sline { start = ssegment.Start } end := segment.Stop if l == eline { end = esegment.Start } node.AppendChild(node, ast.NewRawTextSegment(text.NewSegment(start, end))) if l == eline { block.Advance(end - start) break } else { block.AdvanceLine() } } return node } return nil } func (s *rawHTMLParser) CloseBlock(parent ast.Node, pc Context) { // nothing to do }