package ast
import (
"fmt"
"strings"
textm "github.com/yuin/goldmark/text"
)
type BaseBlock struct {
BaseNode
blankPreviousLines bool
lines *textm .Segments
}
func (b *BaseBlock ) Type () NodeType {
return TypeBlock
}
func (b *BaseBlock ) IsRaw () bool {
return false
}
func (b *BaseBlock ) HasBlankPreviousLines () bool {
return b .blankPreviousLines
}
func (b *BaseBlock ) SetBlankPreviousLines (v bool ) {
b .blankPreviousLines = v
}
func (b *BaseBlock ) Lines () *textm .Segments {
if b .lines == nil {
b .lines = textm .NewSegments ()
}
return b .lines
}
func (b *BaseBlock ) SetLines (v *textm .Segments ) {
b .lines = v
}
type Document struct {
BaseBlock
meta map [string ]interface {}
}
var KindDocument = NewNodeKind ("Document" )
func (n *Document ) Dump (source []byte , level int ) {
DumpHelper (n , source , level , nil , nil )
}
func (n *Document ) Type () NodeType {
return TypeDocument
}
func (n *Document ) Kind () NodeKind {
return KindDocument
}
func (n *Document ) OwnerDocument () *Document {
return n
}
func (n *Document ) Meta () map [string ]interface {} {
if n .meta == nil {
n .meta = map [string ]interface {}{}
}
return n .meta
}
func (n *Document ) SetMeta (meta map [string ]interface {}) {
if n .meta == nil {
n .meta = map [string ]interface {}{}
}
for k , v := range meta {
n .meta [k ] = v
}
}
func (n *Document ) AddMeta (key string , value interface {}) {
if n .meta == nil {
n .meta = map [string ]interface {}{}
}
n .meta [key ] = value
}
func NewDocument () *Document {
return &Document {
BaseBlock : BaseBlock {},
meta : nil ,
}
}
type TextBlock struct {
BaseBlock
}
func (n *TextBlock ) Dump (source []byte , level int ) {
DumpHelper (n , source , level , nil , nil )
}
var KindTextBlock = NewNodeKind ("TextBlock" )
func (n *TextBlock ) Kind () NodeKind {
return KindTextBlock
}
func NewTextBlock () *TextBlock {
return &TextBlock {
BaseBlock : BaseBlock {},
}
}
type Paragraph struct {
BaseBlock
}
func (n *Paragraph ) Dump (source []byte , level int ) {
DumpHelper (n , source , level , nil , nil )
}
var KindParagraph = NewNodeKind ("Paragraph" )
func (n *Paragraph ) Kind () NodeKind {
return KindParagraph
}
func NewParagraph () *Paragraph {
return &Paragraph {
BaseBlock : BaseBlock {},
}
}
func IsParagraph (node Node ) bool {
_ , ok := node .(*Paragraph )
return ok
}
type Heading struct {
BaseBlock
Level int
}
func (n *Heading ) Dump (source []byte , level int ) {
m := map [string ]string {
"Level" : fmt .Sprintf ("%d" , n .Level ),
}
DumpHelper (n , source , level , m , nil )
}
var KindHeading = NewNodeKind ("Heading" )
func (n *Heading ) Kind () NodeKind {
return KindHeading
}
func NewHeading (level int ) *Heading {
return &Heading {
BaseBlock : BaseBlock {},
Level : level ,
}
}
type ThematicBreak struct {
BaseBlock
}
func (n *ThematicBreak ) Dump (source []byte , level int ) {
DumpHelper (n , source , level , nil , nil )
}
var KindThematicBreak = NewNodeKind ("ThematicBreak" )
func (n *ThematicBreak ) Kind () NodeKind {
return KindThematicBreak
}
func NewThematicBreak () *ThematicBreak {
return &ThematicBreak {
BaseBlock : BaseBlock {},
}
}
type CodeBlock struct {
BaseBlock
}
func (n *CodeBlock ) IsRaw () bool {
return true
}
func (n *CodeBlock ) Dump (source []byte , level int ) {
DumpHelper (n , source , level , nil , nil )
}
var KindCodeBlock = NewNodeKind ("CodeBlock" )
func (n *CodeBlock ) Kind () NodeKind {
return KindCodeBlock
}
func NewCodeBlock () *CodeBlock {
return &CodeBlock {
BaseBlock : BaseBlock {},
}
}
type FencedCodeBlock struct {
BaseBlock
Info *Text
language []byte
}
func (n *FencedCodeBlock ) Language (source []byte ) []byte {
if n .language == nil && n .Info != nil {
segment := n .Info .Segment
info := segment .Value (source )
i := 0
for ; i < len (info ); i ++ {
if info [i ] == ' ' {
break
}
}
n .language = info [:i ]
}
return n .language
}
func (n *FencedCodeBlock ) IsRaw () bool {
return true
}
func (n *FencedCodeBlock ) Dump (source []byte , level int ) {
m := map [string ]string {}
if n .Info != nil {
m ["Info" ] = fmt .Sprintf ("\"%s\"" , n .Info .Text (source ))
}
DumpHelper (n , source , level , m , nil )
}
var KindFencedCodeBlock = NewNodeKind ("FencedCodeBlock" )
func (n *FencedCodeBlock ) Kind () NodeKind {
return KindFencedCodeBlock
}
func NewFencedCodeBlock (info *Text ) *FencedCodeBlock {
return &FencedCodeBlock {
BaseBlock : BaseBlock {},
Info : info ,
}
}
type Blockquote struct {
BaseBlock
}
func (n *Blockquote ) Dump (source []byte , level int ) {
DumpHelper (n , source , level , nil , nil )
}
var KindBlockquote = NewNodeKind ("Blockquote" )
func (n *Blockquote ) Kind () NodeKind {
return KindBlockquote
}
func NewBlockquote () *Blockquote {
return &Blockquote {
BaseBlock : BaseBlock {},
}
}
type List struct {
BaseBlock
Marker byte
IsTight bool
Start int
}
func (l *List ) IsOrdered () bool {
return l .Marker == '.' || l .Marker == ')'
}
func (l *List ) CanContinue (marker byte , isOrdered bool ) bool {
return marker == l .Marker && isOrdered == l .IsOrdered ()
}
func (l *List ) Dump (source []byte , level int ) {
m := map [string ]string {
"Ordered" : fmt .Sprintf ("%v" , l .IsOrdered ()),
"Marker" : fmt .Sprintf ("%c" , l .Marker ),
"Tight" : fmt .Sprintf ("%v" , l .IsTight ),
}
if l .IsOrdered () {
m ["Start" ] = fmt .Sprintf ("%d" , l .Start )
}
DumpHelper (l , source , level , m , nil )
}
var KindList = NewNodeKind ("List" )
func (l *List ) Kind () NodeKind {
return KindList
}
func NewList (marker byte ) *List {
return &List {
BaseBlock : BaseBlock {},
Marker : marker ,
IsTight : true ,
}
}
type ListItem struct {
BaseBlock
Offset int
}
func (n *ListItem ) Dump (source []byte , level int ) {
m := map [string ]string {
"Offset" : fmt .Sprintf ("%d" , n .Offset ),
}
DumpHelper (n , source , level , m , nil )
}
var KindListItem = NewNodeKind ("ListItem" )
func (n *ListItem ) Kind () NodeKind {
return KindListItem
}
func NewListItem (offset int ) *ListItem {
return &ListItem {
BaseBlock : BaseBlock {},
Offset : offset ,
}
}
type HTMLBlockType int
const (
HTMLBlockType1 HTMLBlockType = iota + 1
HTMLBlockType2
HTMLBlockType3
HTMLBlockType4
HTMLBlockType5
HTMLBlockType6
HTMLBlockType7
)
type HTMLBlock struct {
BaseBlock
HTMLBlockType HTMLBlockType
ClosureLine textm .Segment
}
func (n *HTMLBlock ) IsRaw () bool {
return true
}
func (n *HTMLBlock ) HasClosure () bool {
return n .ClosureLine .Start >= 0
}
func (n *HTMLBlock ) Dump (source []byte , level int ) {
indent := strings .Repeat (" " , level )
fmt .Printf ("%s%s {\n" , indent , "HTMLBlock" )
indent2 := strings .Repeat (" " , level +1 )
fmt .Printf ("%sRawText: \"" , indent2 )
for i := 0 ; i < n .Lines ().Len (); i ++ {
s := n .Lines ().At (i )
fmt .Print (string (source [s .Start :s .Stop ]))
}
fmt .Printf ("\"\n" )
for c := n .FirstChild (); c != nil ; c = c .NextSibling () {
c .Dump (source , level +1 )
}
if n .HasClosure () {
cl := n .ClosureLine
fmt .Printf ("%sClosure: \"%s\"\n" , indent2 , string (cl .Value (source )))
}
fmt .Printf ("%s}\n" , indent )
}
var KindHTMLBlock = NewNodeKind ("HTMLBlock" )
func (n *HTMLBlock ) Kind () NodeKind {
return KindHTMLBlock
}
func NewHTMLBlock (typ HTMLBlockType ) *HTMLBlock {
return &HTMLBlock {
BaseBlock : BaseBlock {},
HTMLBlockType : typ ,
ClosureLine : textm .NewSegment (-1 , -1 ),
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux 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 @zigo_101 (reachable from the left QR code) to get the latest news of Golds .