| 1 |
package ast |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
|
| 7 |
textm "github.com/yuin/goldmark/text" |
| 8 |
"github.com/yuin/goldmark/util" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
type BaseInline struct { |
| 13 |
BaseNode |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
func (b *BaseInline) Type() NodeType { |
| 18 |
return TypeInline |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
func (b *BaseInline) IsRaw() bool { |
| 23 |
return false |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
func (b *BaseInline) HasBlankPreviousLines() bool { |
| 28 |
panic("can not call with inline nodes.") |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func (b *BaseInline) SetBlankPreviousLines(v bool) { |
| 33 |
panic("can not call with inline nodes.") |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func (b *BaseInline) Lines() *textm.Segments { |
| 38 |
panic("can not call with inline nodes.") |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func (b *BaseInline) SetLines(v *textm.Segments) { |
| 43 |
panic("can not call with inline nodes.") |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
type Text struct { |
| 48 |
BaseInline |
| 49 |
|
| 50 |
Segment textm.Segment |
| 51 |
|
| 52 |
flags uint8 |
| 53 |
} |
| 54 |
|
| 55 |
const ( |
| 56 |
textSoftLineBreak = 1 << iota |
| 57 |
textHardLineBreak |
| 58 |
textRaw |
| 59 |
textCode |
| 60 |
) |
| 61 |
|
| 62 |
func textFlagsString(flags uint8) string { |
| 63 |
buf := []string{} |
| 64 |
if flags&textSoftLineBreak != 0 { |
| 65 |
buf = append(buf, "SoftLineBreak") |
| 66 |
} |
| 67 |
if flags&textHardLineBreak != 0 { |
| 68 |
buf = append(buf, "HardLineBreak") |
| 69 |
} |
| 70 |
if flags&textRaw != 0 { |
| 71 |
buf = append(buf, "Raw") |
| 72 |
} |
| 73 |
if flags&textCode != 0 { |
| 74 |
buf = append(buf, "Code") |
| 75 |
} |
| 76 |
return strings.Join(buf, ", ") |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func (n *Text) Inline() { |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func (n *Text) Pos() int { |
| 85 |
return n.Segment.Start |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
func (n *Text) SoftLineBreak() bool { |
| 91 |
return n.flags&textSoftLineBreak != 0 |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
func (n *Text) SetSoftLineBreak(v bool) { |
| 96 |
if v { |
| 97 |
n.flags |= textSoftLineBreak |
| 98 |
} else { |
| 99 |
n.flags = n.flags &^ textSoftLineBreak |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
func (n *Text) IsRaw() bool { |
| 106 |
return n.flags&textRaw != 0 |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
func (n *Text) SetRaw(v bool) { |
| 111 |
if v { |
| 112 |
n.flags |= textRaw |
| 113 |
} else { |
| 114 |
n.flags = n.flags &^ textRaw |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
func (n *Text) HardLineBreak() bool { |
| 121 |
return n.flags&textHardLineBreak != 0 |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func (n *Text) SetHardLineBreak(v bool) { |
| 126 |
if v { |
| 127 |
n.flags |= textHardLineBreak |
| 128 |
} else { |
| 129 |
n.flags = n.flags &^ textHardLineBreak |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
func (n *Text) Merge(node Node, source []byte) bool { |
| 136 |
t, ok := node.(*Text) |
| 137 |
if !ok { |
| 138 |
return false |
| 139 |
} |
| 140 |
if n.Segment.Stop != t.Segment.Start || t.Segment.Padding != 0 || |
| 141 |
source[n.Segment.Stop-1] == '\n' || t.IsRaw() != n.IsRaw() { |
| 142 |
return false |
| 143 |
} |
| 144 |
n.Segment.Stop = t.Segment.Stop |
| 145 |
n.SetSoftLineBreak(t.SoftLineBreak()) |
| 146 |
n.SetHardLineBreak(t.HardLineBreak()) |
| 147 |
return true |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
func (n *Text) Text(source []byte) []byte { |
| 154 |
return n.Segment.Value(source) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
func (n *Text) Value(source []byte) []byte { |
| 160 |
return n.Segment.Value(source) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
func (n *Text) Dump(source []byte, level int) { |
| 165 |
m := map[string]string{ |
| 166 |
"Value": "\"" + strings.TrimRight(string(n.Value(source)), "\n") + "\"", |
| 167 |
} |
| 168 |
fs := textFlagsString(n.flags) |
| 169 |
if len(fs) != 0 { |
| 170 |
m["Flags"] = fs |
| 171 |
} |
| 172 |
DumpHelper(n, source, level, m, nil) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
var KindText = NewNodeKind("Text") |
| 177 |
|
| 178 |
|
| 179 |
func (n *Text) Kind() NodeKind { |
| 180 |
return KindText |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
func NewText() *Text { |
| 185 |
return &Text{ |
| 186 |
BaseInline: BaseInline{}, |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
func NewTextSegment(v textm.Segment) *Text { |
| 192 |
return &Text{ |
| 193 |
BaseInline: BaseInline{}, |
| 194 |
Segment: v, |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
func NewRawTextSegment(v textm.Segment) *Text { |
| 201 |
t := &Text{ |
| 202 |
BaseInline: BaseInline{}, |
| 203 |
Segment: v, |
| 204 |
} |
| 205 |
t.SetRaw(true) |
| 206 |
return t |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
func MergeOrAppendTextSegment(parent Node, s textm.Segment) { |
| 213 |
last := parent.LastChild() |
| 214 |
t, ok := last.(*Text) |
| 215 |
if ok && t.Segment.Stop == s.Start && !t.SoftLineBreak() { |
| 216 |
t.Segment = t.Segment.WithStop(s.Stop) |
| 217 |
} else { |
| 218 |
parent.AppendChild(parent, NewTextSegment(s)) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
func MergeOrReplaceTextSegment(parent Node, n Node, s textm.Segment) { |
| 225 |
prev := n.PreviousSibling() |
| 226 |
if t, ok := prev.(*Text); ok && t.Segment.Stop == s.Start && !t.SoftLineBreak() { |
| 227 |
t.Segment = t.Segment.WithStop(s.Stop) |
| 228 |
parent.RemoveChild(parent, n) |
| 229 |
} else { |
| 230 |
parent.ReplaceChild(parent, n, NewTextSegment(s)) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
type String struct { |
| 236 |
BaseInline |
| 237 |
|
| 238 |
Value []byte |
| 239 |
flags uint8 |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
func (n *String) Inline() { |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
func (n *String) Pos() int { |
| 249 |
return -1 |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
func (n *String) IsRaw() bool { |
| 255 |
return n.flags&textRaw != 0 |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
func (n *String) SetRaw(v bool) { |
| 260 |
if v { |
| 261 |
n.flags |= textRaw |
| 262 |
} else { |
| 263 |
n.flags = n.flags &^ textRaw |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
func (n *String) IsCode() bool { |
| 270 |
return n.flags&textCode != 0 |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
func (n *String) SetCode(v bool) { |
| 275 |
if v { |
| 276 |
n.flags |= textCode |
| 277 |
} else { |
| 278 |
n.flags = n.flags &^ textCode |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
func (n *String) Text(source []byte) []byte { |
| 286 |
return n.Value |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
func (n *String) Dump(source []byte, level int) { |
| 291 |
fs := textFlagsString(n.flags) |
| 292 |
if len(fs) != 0 { |
| 293 |
fs = "(" + fs + ")" |
| 294 |
} |
| 295 |
fmt.Printf("%sString%s: \"%s\"\n", strings.Repeat(" ", level), fs, strings.TrimRight(string(n.Value), "\n")) |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
var KindString = NewNodeKind("String") |
| 300 |
|
| 301 |
|
| 302 |
func (n *String) Kind() NodeKind { |
| 303 |
return KindString |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
func NewString(v []byte) *String { |
| 308 |
return &String{ |
| 309 |
Value: v, |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
type CodeSpan struct { |
| 315 |
BaseInline |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
func (n *CodeSpan) Inline() { |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
func (n *CodeSpan) IsBlank(source []byte) bool { |
| 324 |
for c := n.FirstChild(); c != nil; c = c.NextSibling() { |
| 325 |
text := c.(*Text).Segment |
| 326 |
if !util.IsBlank(text.Value(source)) { |
| 327 |
return false |
| 328 |
} |
| 329 |
} |
| 330 |
return true |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
func (n *CodeSpan) Dump(source []byte, level int) { |
| 335 |
DumpHelper(n, source, level, nil, nil) |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
var KindCodeSpan = NewNodeKind("CodeSpan") |
| 340 |
|
| 341 |
|
| 342 |
func (n *CodeSpan) Kind() NodeKind { |
| 343 |
return KindCodeSpan |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
func NewCodeSpan() *CodeSpan { |
| 348 |
return &CodeSpan{ |
| 349 |
BaseInline: BaseInline{}, |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
type Emphasis struct { |
| 355 |
BaseInline |
| 356 |
|
| 357 |
|
| 358 |
Level int |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
func (n *Emphasis) Dump(source []byte, level int) { |
| 363 |
m := map[string]string{ |
| 364 |
"Level": fmt.Sprintf("%v", n.Level), |
| 365 |
} |
| 366 |
DumpHelper(n, source, level, m, nil) |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
var KindEmphasis = NewNodeKind("Emphasis") |
| 371 |
|
| 372 |
|
| 373 |
func (n *Emphasis) Kind() NodeKind { |
| 374 |
return KindEmphasis |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
func NewEmphasis(level int) *Emphasis { |
| 379 |
return &Emphasis{ |
| 380 |
BaseInline: BaseInline{}, |
| 381 |
Level: level, |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
type baseLink struct { |
| 386 |
BaseInline |
| 387 |
|
| 388 |
|
| 389 |
Destination []byte |
| 390 |
|
| 391 |
|
| 392 |
Title []byte |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
Reference *ReferenceLink |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
func (n *baseLink) Inline() { |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
type ReferenceLinkType int |
| 405 |
|
| 406 |
const ( |
| 407 |
|
| 408 |
ReferenceLinkFull ReferenceLinkType = iota + 1 |
| 409 |
|
| 410 |
ReferenceLinkCollapsed |
| 411 |
|
| 412 |
ReferenceLinkShortcut |
| 413 |
) |
| 414 |
|
| 415 |
|
| 416 |
func (t ReferenceLinkType) String() string { |
| 417 |
switch t { |
| 418 |
case ReferenceLinkFull: |
| 419 |
return "Full" |
| 420 |
case ReferenceLinkCollapsed: |
| 421 |
return "Collapsed" |
| 422 |
case ReferenceLinkShortcut: |
| 423 |
return "Shortcut" |
| 424 |
default: |
| 425 |
return fmt.Sprintf("Unknown(%d)", t) |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
type ReferenceLink struct { |
| 431 |
|
| 432 |
Type ReferenceLinkType |
| 433 |
|
| 434 |
|
| 435 |
Value []byte |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
func NewReferenceLink(typ ReferenceLinkType, value []byte) *ReferenceLink { |
| 440 |
return &ReferenceLink{ |
| 441 |
Type: typ, |
| 442 |
Value: value, |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
type Link struct { |
| 448 |
baseLink |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
func (n *Link) Dump(source []byte, level int) { |
| 453 |
m := map[string]string{} |
| 454 |
m["Destination"] = string(n.Destination) |
| 455 |
if len(n.Title) != 0 { |
| 456 |
m["Title"] = string(n.Title) |
| 457 |
} |
| 458 |
cb := func(int) {} |
| 459 |
if n.Reference != nil { |
| 460 |
cb = func(level int) { |
| 461 |
indent := strings.Repeat(" ", level) |
| 462 |
fmt.Printf("%sReference {\n", indent) |
| 463 |
indent2 := strings.Repeat(" ", level+1) |
| 464 |
fmt.Printf("%sType : %s\n", indent2, n.Reference.Type.String()) |
| 465 |
fmt.Printf("%sValue : %s\n", indent2, string(n.Reference.Value)) |
| 466 |
fmt.Printf("%s}\n", indent) |
| 467 |
|
| 468 |
} |
| 469 |
} |
| 470 |
DumpHelper(n, source, level, m, cb) |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
var KindLink = NewNodeKind("Link") |
| 475 |
|
| 476 |
|
| 477 |
func (n *Link) Kind() NodeKind { |
| 478 |
return KindLink |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
func NewLink() *Link { |
| 483 |
c := &Link{ |
| 484 |
baseLink: baseLink{ |
| 485 |
BaseInline: BaseInline{}, |
| 486 |
}, |
| 487 |
} |
| 488 |
return c |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
type Image struct { |
| 493 |
baseLink |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
func (n *Image) Dump(source []byte, level int) { |
| 498 |
m := map[string]string{} |
| 499 |
m["Destination"] = string(n.Destination) |
| 500 |
if len(n.Title) != 0 { |
| 501 |
m["Title"] = string(n.Title) |
| 502 |
} |
| 503 |
cb := func(int) {} |
| 504 |
if n.Reference != nil { |
| 505 |
cb = func(level int) { |
| 506 |
indent := strings.Repeat(" ", level) |
| 507 |
fmt.Printf("%sReference {\n", indent) |
| 508 |
indent2 := strings.Repeat(" ", level+1) |
| 509 |
fmt.Printf("%sType : %s\n", indent2, n.Reference.Type.String()) |
| 510 |
fmt.Printf("%sValue : %s\n", indent2, string(n.Reference.Value)) |
| 511 |
fmt.Printf("%s}\n", indent) |
| 512 |
|
| 513 |
} |
| 514 |
} |
| 515 |
DumpHelper(n, source, level, m, cb) |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
var KindImage = NewNodeKind("Image") |
| 520 |
|
| 521 |
|
| 522 |
func (n *Image) Kind() NodeKind { |
| 523 |
return KindImage |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
func NewImage(link *Link) *Image { |
| 528 |
c := &Image{ |
| 529 |
baseLink: baseLink{ |
| 530 |
BaseInline: BaseInline{}, |
| 531 |
}, |
| 532 |
} |
| 533 |
c.Destination = link.Destination |
| 534 |
c.Title = link.Title |
| 535 |
c.Reference = link.Reference |
| 536 |
for n := link.FirstChild(); n != nil; { |
| 537 |
next := n.NextSibling() |
| 538 |
link.RemoveChild(link, n) |
| 539 |
c.AppendChild(c, n) |
| 540 |
n = next |
| 541 |
} |
| 542 |
|
| 543 |
return c |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
type AutoLinkType int |
| 548 |
|
| 549 |
const ( |
| 550 |
|
| 551 |
AutoLinkEmail AutoLinkType = iota + 1 |
| 552 |
|
| 553 |
AutoLinkURL |
| 554 |
) |
| 555 |
|
| 556 |
|
| 557 |
type AutoLink struct { |
| 558 |
BaseInline |
| 559 |
|
| 560 |
AutoLinkType AutoLinkType |
| 561 |
|
| 562 |
|
| 563 |
Protocol []byte |
| 564 |
|
| 565 |
value *Text |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
func (n *AutoLink) Inline() {} |
| 570 |
|
| 571 |
|
| 572 |
func (n *AutoLink) Dump(source []byte, level int) { |
| 573 |
segment := n.value.Segment |
| 574 |
m := map[string]string{ |
| 575 |
"Value": string(segment.Value(source)), |
| 576 |
} |
| 577 |
DumpHelper(n, source, level, m, nil) |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
var KindAutoLink = NewNodeKind("AutoLink") |
| 582 |
|
| 583 |
|
| 584 |
func (n *AutoLink) Kind() NodeKind { |
| 585 |
return KindAutoLink |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
func (n *AutoLink) URL(source []byte) []byte { |
| 590 |
if n.Protocol != nil { |
| 591 |
s := n.value.Segment |
| 592 |
ret := make([]byte, 0, len(n.Protocol)+s.Len()+3) |
| 593 |
ret = append(ret, n.Protocol...) |
| 594 |
ret = append(ret, ':', '/', '/') |
| 595 |
ret = append(ret, n.value.Value(source)...) |
| 596 |
return ret |
| 597 |
} |
| 598 |
return n.value.Value(source) |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
func (n *AutoLink) Label(source []byte) []byte { |
| 603 |
return n.value.Value(source) |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
func (n *AutoLink) Text(source []byte) []byte { |
| 610 |
return n.value.Value(source) |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
func NewAutoLink(typ AutoLinkType, value *Text) *AutoLink { |
| 615 |
return &AutoLink{ |
| 616 |
BaseInline: BaseInline{}, |
| 617 |
value: value, |
| 618 |
AutoLinkType: typ, |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
type RawHTML struct { |
| 624 |
BaseInline |
| 625 |
Segments *textm.Segments |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
func (n *RawHTML) Inline() {} |
| 630 |
|
| 631 |
|
| 632 |
func (n *RawHTML) Dump(source []byte, level int) { |
| 633 |
m := map[string]string{} |
| 634 |
t := []string{} |
| 635 |
for i := range n.Segments.Len() { |
| 636 |
segment := n.Segments.At(i) |
| 637 |
t = append(t, string(segment.Value(source))) |
| 638 |
} |
| 639 |
m["RawText"] = strings.Join(t, "") |
| 640 |
DumpHelper(n, source, level, m, nil) |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
var KindRawHTML = NewNodeKind("RawHTML") |
| 645 |
|
| 646 |
|
| 647 |
func (n *RawHTML) Kind() NodeKind { |
| 648 |
return KindRawHTML |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
func (n *RawHTML) Text(source []byte) []byte { |
| 655 |
return n.Segments.Value(source) |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
func NewRawHTML() *RawHTML { |
| 660 |
return &RawHTML{ |
| 661 |
Segments: textm.NewSegments(), |
| 662 |
} |
| 663 |
} |