| |
| |
| |
|
|
| package markdown |
|
|
| import ( |
| "bytes" |
| "strings" |
| ) |
|
|
| type Empty struct { |
| Position |
| } |
|
|
| func (b *Empty) PrintHTML(buf *bytes.Buffer) {} |
|
|
| func (b *Empty) printMarkdown(*bytes.Buffer, mdState) {} |
|
|
| type Paragraph struct { |
| Position |
| Text *Text |
| } |
|
|
| func (b *Paragraph) PrintHTML(buf *bytes.Buffer) { |
| buf.WriteString("<p>") |
| b.Text.PrintHTML(buf) |
| buf.WriteString("</p>\n") |
| } |
|
|
| func (b *Paragraph) printMarkdown(buf *bytes.Buffer, s mdState) { |
| |
| |
| |
| |
| b.Text.printMarkdown(buf, s) |
| } |
|
|
| type paraBuilder struct { |
| text []string |
| table *tableBuilder |
| } |
|
|
| func (b *paraBuilder) extend(p *parseState, s line) (line, bool) { |
| return s, false |
| } |
|
|
| func (b *paraBuilder) build(p buildState) Block { |
| if b.table != nil { |
| return b.table.build(p) |
| } |
|
|
| s := strings.Join(b.text, "\n") |
| for s != "" { |
| end, ok := parseLinkRefDef(p, s) |
| if !ok { |
| break |
| } |
| s = s[skipSpace(s, end):] |
| } |
|
|
| if s == "" { |
| return &Empty{p.pos()} |
| } |
|
|
| |
| |
| pos := p.pos() |
| pos.EndLine = pos.StartLine + len(b.text) - 1 |
| return &Paragraph{ |
| pos, |
| p.newText(pos, s), |
| } |
| } |
|
|
| func newPara(p *parseState, s line) (line, bool) { |
| |
| b := p.para() |
| indented := p.lineDepth == len(p.stack)-2 |
| text := s.trimSpaceString() |
|
|
| if b != nil && b.table != nil { |
| if indented && text != "" && text != "|" { |
| |
| b.table.addRow(text) |
| return line{}, true |
| } |
| |
| |
| |
| |
| |
| |
| |
| b = nil |
| } |
|
|
| |
| if p.Table && b != nil && indented && len(b.text) > 0 && isTableStart(b.text[len(b.text)-1], text) { |
| hdr := b.text[len(b.text)-1] |
| b.text = b.text[:len(b.text)-1] |
| tb := new(paraBuilder) |
| p.addBlock(tb) |
| tb.table = new(tableBuilder) |
| tb.table.start(hdr, text) |
| return line{}, true |
| } |
|
|
| if b != nil { |
| for i := p.lineDepth; i < len(p.stack); i++ { |
| p.stack[i].pos.EndLine = p.lineno |
| } |
| } else { |
| |
| b = new(paraBuilder) |
| p.addBlock(b) |
| } |
| b.text = append(b.text, text) |
| return line{}, true |
| } |
|
|