Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Make API public to create other Formatters #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions elemheap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ field.
*/

type elemHeap struct {
rows [][]element
rows [][]Element
heapPos
rowSize int

Expand All @@ -24,21 +24,21 @@ type elemHeap struct {

type heapPos struct {
iRow int
row []element
row []Element
}

func (h *elemHeap) nextRow() []element {
func (h *elemHeap) nextRow() []Element {
h.iRow++
if h.iRow == len(h.rows) {
h.rows = append(h.rows, make([]element, h.rowSize))
h.rows = append(h.rows, make([]Element, h.rowSize))
}
h.row = h.rows[h.iRow]
return h.row
}

func (h *elemHeap) init(size int) {
h.rowSize = size
h.rows = [][]element{make([]element, size)}
h.rows = [][]Element{make([]Element, size)}
h.row = h.rows[h.iRow]
h.base = h.heapPos
}
Expand Down
34 changes: 17 additions & 17 deletions markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewParser(x *Extensions) (p *Parser) {
// method is called, which may, for example, print footnotes.
// A Formatter can be reused.
type Formatter interface {
FormatBlock(*element)
FormatBlock(*Element)
Finish()
}

Expand Down Expand Up @@ -97,7 +97,7 @@ func (p *Parser) Markdown(src io.Reader, f Formatter) {
f.Finish()
}

func (p *Parser) parseRule(rule int, s string) (tree *element) {
func (p *Parser) parseRule(rule int, s string) (tree *Element) {
old := p.yy.ResetBuffer(s)
if old != "" && strings.Trim(old, "\r\n ") != "" {
log.Fatalln("Buffer not empty", "["+old+"]")
Expand All @@ -113,34 +113,34 @@ func (p *Parser) parseRule(rule int, s string) (tree *element) {
return
}

/* process_raw_blocks - traverses an element list, replacing any RAW elements with
/* process_raw_blocks - traverses an Element list, replacing any RAW elements with
* the result of parsing them as markdown text, and recursing into the children
* of parent elements. The result should be a tree of elements without any RAWs.
*/
func (p *Parser) processRawBlocks(input *element) *element {
func (p *Parser) processRawBlocks(input *Element) *Element {

for current := input; current != nil; current = current.next {
if current.key == RAW {
for current := input; current != nil; current = current.Next {
if current.Key == RAW {
/* \001 is used to indicate boundaries between nested lists when there
* is no blank line. We split the string by \001 and parse
* each chunk separately.
*/
current.key = LIST
current.children = nil
listEnd := &current.children
for _, contents := range strings.Split(current.contents.str, "\001") {
if list := p.parseRule(ruleDoc, contents); list != nil {
current.Key = LIST
current.Children = nil
listEnd := &current.Children
for _, Contents := range strings.Split(current.Contents.Str, "\001") {
if list := p.parseRule(ruleDoc, Contents); list != nil {
*listEnd = list
for list.next != nil {
list = list.next
for list.Next != nil {
list = list.Next
}
listEnd = &list.next
listEnd = &list.Next
}
}
current.contents.str = ""
current.Contents.Str = ""
}
if current.children != nil {
current.children = p.processRawBlocks(current.children)
if current.Children != nil {
current.Children = p.processRawBlocks(current.Children)
}
}
return input
Expand Down
4 changes: 2 additions & 2 deletions misc/c2go.sed
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ s,find_reference,p.findReference,g

s,->key,.key,g
s,->children,.children,g
s,->contents.str,.contents.str,g
s,->Contents.Str,.Contents.Str,g

/{ *if (extens/ {
s,if (,if ,
Expand All @@ -30,7 +30,7 @@ s,->contents.str,.contents.str,g
s,extension.EXT_NOTES.,extension.Notes,g
}

s,{ *element \*[a-z]*\; *$,{,
s,{ *Element \*[a-z]*\; *$,{,

/raw\.key =/ s,;$,,
/result =/ s,;$,,
Expand Down
40 changes: 20 additions & 20 deletions out-groffmm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func ToGroffMM(w Writer) Formatter {
f.escape = strings.NewReplacer(`\`, `\e`)
return f
}
func (f *troffOut) FormatBlock(tree *element) {
func (f *troffOut) FormatBlock(tree *Element) {
f.elist(tree)
}
func (f *troffOut) Finish() {
Expand Down Expand Up @@ -76,10 +76,10 @@ func (w *troffOut) str(s string) *troffOut {
return w
}

func (w *troffOut) children(el *element) *troffOut {
return w.elist(el.children)
func (w *troffOut) children(el *Element) *troffOut {
return w.elist(el.Children)
}
func (w *troffOut) inline(pfx string, el *element, sfx string) *troffOut {
func (w *troffOut) inline(pfx string, el *Element, sfx string) *troffOut {
return w.s(pfx).children(el).s(sfx)
}

Expand All @@ -88,24 +88,24 @@ func (w *troffOut) req(name string) *troffOut {
}

// write a list of elements
func (w *troffOut) elist(list *element) *troffOut {
func (w *troffOut) elist(list *Element) *troffOut {
for i := 0; list != nil; i++ {
w.elem(list, i == 0)
list = list.next
list = list.Next
}
return w
}

func (w *troffOut) elem(elt *element, isFirst bool) *troffOut {
func (w *troffOut) elem(elt *Element, isFirst bool) *troffOut {
var s string

switch elt.key {
switch elt.Key {
case SPACE:
s = elt.contents.str
s = elt.Contents.Str
case LINEBREAK:
w.req("br\n")
case STR:
w.str(elt.contents.str)
w.str(elt.Contents.Str)
case ELLIPSIS:
s = "..."
case EMDASH:
Expand All @@ -119,15 +119,15 @@ func (w *troffOut) elem(elt *element, isFirst bool) *troffOut {
case DOUBLEQUOTED:
w.inline(`\[lq]`, elt, `\[rq]`)
case CODE:
w.s(`\fC`).str(elt.contents.str).s(`\fR`)
w.s(`\fC`).str(elt.Contents.Str).s(`\fR`)
case HTML:
/* don't print HTML */
case LINK:
link := elt.contents.link
w.elist(link.label)
w.s(" (").s(link.url).s(")")
link := elt.Contents.Link
w.elist(link.Label)
w.s(" (").s(link.URL).s(")")
case IMAGE:
w.s("[IMAGE: ").elist(elt.contents.link.label).s("]")
w.s("[IMAGE: ").elist(elt.Contents.Link.Label).s("]")
/* not supported */
case EMPH:
w.inline(`\fI`, elt, `\fR`)
Expand All @@ -150,7 +150,7 @@ func (w *troffOut) elem(elt *element, isFirst bool) *troffOut {
/* Shouldn't occur - these are handled by process_raw_blocks() */
log.Fatalf("RAW")
case H1, H2, H3, H4, H5, H6:
h := ".H " + string('1'+elt.key-H1) + ` "` /* assumes H1 ... H6 are in order */
h := ".H " + string('1'+elt.Key-H1) + ` "` /* assumes H1 ... H6 are in order */
w.br().inline(h, elt, `"`)
case PLAIN:
w.br().children(elt)
Expand All @@ -166,7 +166,7 @@ func (w *troffOut) elem(elt *element, isFirst bool) *troffOut {
/* don't print HTML block */
case VERBATIM:
w.req("VERBON 2\n")
w.str(elt.contents.str)
w.str(elt.Contents.Str)
w.s(".VERBOFF")
case BULLETLIST:
w.req("BL").children(elt).req("LE 1")
Expand All @@ -191,9 +191,9 @@ func (w *troffOut) elem(elt *element, isFirst bool) *troffOut {
w.children(elt)
w.req("DE")
case NOTE:
/* if contents.str == 0, then print note; else ignore, since this
/* if Contents.Str == 0, then print note; else ignore, since this
* is a note block that has been incorporated into the notes list */
if elt.contents.str == "" {
if elt.Contents.Str == "" {
w.s("\\*F\n")
w.s(".FS\n")
w.skipPadding()
Expand All @@ -203,7 +203,7 @@ func (w *troffOut) elem(elt *element, isFirst bool) *troffOut {
case REFERENCE:
/* Nonprinting */
default:
log.Fatalf("troffOut.elem encountered unknown element key = %d\n", elt.key)
log.Fatalf("troffOut.elem encountered unknown Element key = %d\n", elt.Key)
}
if s != "" {
w.s(s)
Expand Down
66 changes: 33 additions & 33 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ type htmlOut struct {
obfuscate bool

notenum int
endNotes []*element /* List of endnotes to print after main content. */
endNotes []*Element /* List of endnotes to print after main content. */
}

func ToHTML(w Writer) Formatter {
f := new(htmlOut)
f.baseWriter = baseWriter{w, 2}
return f
}
func (f *htmlOut) FormatBlock(tree *element) {
func (f *htmlOut) FormatBlock(tree *Element) {
f.elist(tree)
}
func (f *htmlOut) Finish() {
Expand Down Expand Up @@ -140,40 +140,40 @@ func (w *htmlOut) str(s string) *htmlOut {
return w
}

func (w *htmlOut) children(el *element) *htmlOut {
return w.elist(el.children)
func (w *htmlOut) children(el *Element) *htmlOut {
return w.elist(el.Children)
}
func (w *htmlOut) inline(tag string, el *element) *htmlOut {
func (w *htmlOut) inline(tag string, el *Element) *htmlOut {
return w.s(tag).children(el).s("</").s(tag[1:])
}
func (w *htmlOut) listBlock(tag string, el *element) *htmlOut {
return w.sp().s(tag).elist(el.children).br().s("</").s(tag[1:])
func (w *htmlOut) listBlock(tag string, el *Element) *htmlOut {
return w.sp().s(tag).elist(el.Children).br().s("</").s(tag[1:])
}
func (w *htmlOut) listItem(tag string, el *element) *htmlOut {
return w.br().s(tag).skipPadding().elist(el.children).s("</").s(tag[1:])
func (w *htmlOut) listItem(tag string, el *Element) *htmlOut {
return w.br().s(tag).skipPadding().elist(el.Children).s("</").s(tag[1:])
}

/* print a list of elements
*/
func (w *htmlOut) elist(list *element) *htmlOut {
func (w *htmlOut) elist(list *Element) *htmlOut {
for list != nil {
w.elem(list)
list = list.next
list = list.Next
}
return w
}

// print an element
func (w *htmlOut) elem(elt *element) *htmlOut {
// print an Element
func (w *htmlOut) elem(elt *Element) *htmlOut {
var s string

switch elt.key {
switch elt.Key {
case SPACE:
s = elt.contents.str
s = elt.Contents.Str
case LINEBREAK:
s = "<br/>\n"
case STR:
w.str(elt.contents.str)
w.str(elt.Contents.Str)
case ELLIPSIS:
s = "&hellip;"
case EMDASH:
Expand All @@ -187,25 +187,25 @@ func (w *htmlOut) elem(elt *element) *htmlOut {
case DOUBLEQUOTED:
w.s("&ldquo;").children(elt).s("&rdquo;")
case CODE:
w.s("<code>").str(elt.contents.str).s("</code>")
w.s("<code>").str(elt.Contents.Str).s("</code>")
case HTML:
s = elt.contents.str
s = elt.Contents.Str
case LINK:
o := w.obfuscate
if strings.Index(elt.contents.link.url, "mailto:") == 0 {
if strings.Index(elt.Contents.Link.URL, "mailto:") == 0 {
w.obfuscate = true /* obfuscate mailto: links */
}
w.s(`<a href="`).str(elt.contents.link.url).s(`"`)
if len(elt.contents.link.title) > 0 {
w.s(` title="`).str(elt.contents.link.title).s(`"`)
w.s(`<a href="`).str(elt.Contents.Link.URL).s(`"`)
if len(elt.Contents.Link.Title) > 0 {
w.s(` title="`).str(elt.Contents.Link.Title).s(`"`)
}
w.s(">").elist(elt.contents.link.label).s("</a>")
w.s(">").elist(elt.Contents.Link.Label).s("</a>")
w.obfuscate = o
case IMAGE:
w.s(`<img src="`).str(elt.contents.link.url).s(`" alt="`)
w.elist(elt.contents.link.label).s(`"`)
if len(elt.contents.link.title) > 0 {
w.s(` title="`).str(elt.contents.link.title).s(`"`)
w.s(`<img src="`).str(elt.Contents.Link.URL).s(`" alt="`)
w.elist(elt.Contents.Link.Label).s(`"`)
if len(elt.Contents.Link.Title) > 0 {
w.s(` title="`).str(elt.Contents.Link.Title).s(`"`)
}
w.s(" />")
case EMPH:
Expand All @@ -220,7 +220,7 @@ func (w *htmlOut) elem(elt *element) *htmlOut {
/* Shouldn't occur - these are handled by process_raw_blocks() */
log.Fatalf("RAW")
case H1, H2, H3, H4, H5, H6:
h := "<h" + string('1'+elt.key-H1) + ">" /* assumes H1 ... H6 are in order */
h := "<h" + string('1'+elt.Key-H1) + ">" /* assumes H1 ... H6 are in order */
w.sp().inline(h, elt)
case PLAIN:
w.br().children(elt)
Expand All @@ -229,9 +229,9 @@ func (w *htmlOut) elem(elt *element) *htmlOut {
case HRULE:
w.sp().s("<hr />")
case HTMLBLOCK:
w.sp().s(elt.contents.str)
w.sp().s(elt.Contents.Str)
case VERBATIM:
w.sp().s("<pre><code>").str(elt.contents.str).s("</code></pre>")
w.sp().s("<pre><code>").str(elt.Contents.Str).s("</code></pre>")
case BULLETLIST:
w.listBlock("<ul>", elt)
case ORDEREDLIST:
Expand All @@ -249,18 +249,18 @@ func (w *htmlOut) elem(elt *element) *htmlOut {
case REFERENCE:
/* Nonprinting */
case NOTE:
/* if contents.str == 0, then print note; else ignore, since this
/* if Contents.Str == 0, then print note; else ignore, since this
* is a note block that has been incorporated into the notes list
*/
if elt.contents.str == "" {
if elt.Contents.Str == "" {
w.endNotes = append(w.endNotes, elt) /* add an endnote to global endnotes list */
w.notenum++
nn := w.notenum
s = fmt.Sprintf(`<a class="noteref" id="fnref%d" href="#fn%d" title="Jump to note %d">[%d]</a>`,
nn, nn, nn, nn)
}
default:
log.Fatalf("htmlOut.elem encountered unknown element key = %d\n", elt.key)
log.Fatalf("htmlOut.elem encountered unknown Element key = %d\n", elt.Key)
}
if s != "" {
w.s(s)
Expand Down
Loading