From 4d3a0999222efbfe37372f51b245951b85086a37 Mon Sep 17 00:00:00 2001 From: Ori Date: Tue, 22 Jun 2021 16:57:09 -0400 Subject: [PATCH] For simple list items, don't wrap in

tags This also removes extra newlines for simple list items, see changes to tests for details. Closes #57 --- org/html_writer.go | 33 ++++++- org/testdata/blocks.html | 20 ++-- org/testdata/footnotes.html | 30 ++---- org/testdata/footnotes_in_headline.html | 20 ++-- org/testdata/headlines.html | 14 +-- org/testdata/inline.html | 126 +++++++----------------- org/testdata/latex.html | 30 ++---- org/testdata/lists.html | 120 ++++++---------------- org/testdata/misc.html | 72 ++++---------- 9 files changed, 146 insertions(+), 319 deletions(-) diff --git a/org/html_writer.go b/org/html_writer.go index 08a5b69..878560b 100644 --- a/org/html_writer.go +++ b/org/html_writer.go @@ -4,6 +4,7 @@ import ( "fmt" "html" "log" + "reflect" "regexp" "strconv" "strings" @@ -415,8 +416,8 @@ func (w *HTMLWriter) WriteListItem(li ListItem) { if li.Status != "" { attributes += fmt.Sprintf(` class="%s"`, listItemStatuses[li.Status]) } - w.WriteString(fmt.Sprintf("\n", attributes)) - WriteNodes(w, li.Children...) + w.WriteString(fmt.Sprintf("", attributes)) + w.writeListItemContent(li.Children) w.WriteString("\n") } @@ -433,11 +434,26 @@ func (w *HTMLWriter) WriteDescriptiveListItem(di DescriptiveListItem) { w.WriteString("?") } w.WriteString("\n\n") - w.WriteString("

\n") - WriteNodes(w, di.Details...) + w.WriteString("
") + w.writeListItemContent(di.Details) w.WriteString("
\n") } +func (w *HTMLWriter) writeListItemContent(children []Node) { + if isParagraphNodeSlice(children) { + for i, c := range children { + out := w.WriteNodesAsString(c.(Paragraph).Children...) + if i != 0 && out != "" { + w.WriteString("\n") + } + w.WriteString(out) + } + } else { + w.WriteString("\n") + WriteNodes(w, children...) + } +} + func (w *HTMLWriter) WriteParagraph(p Paragraph) { if len(p.Children) == 0 { return @@ -585,6 +601,15 @@ func setHTMLAttribute(attributes []h.Attribute, k, v string) []h.Attribute { return append(attributes, h.Attribute{Namespace: "", Key: k, Val: v}) } +func isParagraphNodeSlice(ns []Node) bool { + for _, n := range ns { + if reflect.TypeOf(n).Name() != "Paragraph" { + return false + } + } + return true +} + func (fs *footnotes) add(f FootnoteLink) int { if i, ok := fs.mapping[f.Name]; ok && f.Name != "" { return i diff --git a/org/testdata/blocks.html b/org/testdata/blocks.html index 20c7746..a020dbc 100644 --- a/org/testdata/blocks.html +++ b/org/testdata/blocks.html @@ -68,12 +68,8 @@ note that /inline/ *markup* ignored

blocks like the quote block parse their content and can contain

diff --git a/org/testdata/footnotes.html b/org/testdata/footnotes.html index 4b6b9e4..cb9d7d5 100644 --- a/org/testdata/footnotes.html +++ b/org/testdata/footnotes.html @@ -12,25 +12,13 @@ Using some footnotes
    -
  • -

    normal footnote reference 1 2 3 (footnote names can be anything in the format [\w-])

    -
  • -
  • -

    further references to the same footnote should not 1 render duplicates in the footnote list

    -
  • -
  • -

    inline footnotes are also supported via 4.

    -
  • -
  • -

    anonymous inline footnotes are also supported via 5.

    -
  • -
  • -

    Footnote definitions are not printed where they appear. -Rather, they are gathered and exported at the end of the document in the footnote section. 6

    -
  • -
  • -

    footnotes that reference a non-existant definition are rendered but log a warning 7

    -
  • +
  • normal footnote reference 1 2 3 (footnote names can be anything in the format [\w-])
  • +
  • further references to the same footnote should not 1 render duplicates in the footnote list
  • +
  • inline footnotes are also supported via 4.
  • +
  • anonymous inline footnotes are also supported via 5.
  • +
  • Footnote definitions are not printed where they appear. +Rather, they are gathered and exported at the end of the document in the footnote section. 6
  • +
  • footnotes that reference a non-existant definition are rendered but log a warning 7
@@ -52,9 +40,7 @@ this is not part of

https://www.example.com