diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-11-16 03:14:22 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-11-16 03:14:32 +0700 |
| commit | 4f24d95fa5cb8c7db3e4d123460f83fa2b489cdf (patch) | |
| tree | 06d22a2982e1a15732ebd7740dfcc157de9aa3da | |
| parent | 18f26d69d3ac10445420f6a7cf2a1ac5111786ff (diff) | |
| download | asciidoctor-go-4f24d95fa5cb8c7db3e4d123460f83fa2b489cdf.tar.xz | |
all: support forcing line break by ending the line with " +\n"
| -rw-r--r-- | README.adoc | 5 | ||||
| -rw-r--r-- | adoc_node.go | 12 | ||||
| -rw-r--r-- | parser_inline.go | 17 | ||||
| -rw-r--r-- | testdata/got.test.html | 59 | ||||
| -rw-r--r-- | testdata/test.adoc | 24 | ||||
| -rw-r--r-- | testdata/test.html | 45 |
6 files changed, 149 insertions, 13 deletions
diff --git a/README.adoc b/README.adoc index d9f42b3..0886764 100644 --- a/README.adoc +++ b/README.adoc @@ -36,6 +36,8 @@ https://asciidoctor.org/docs/user-manual/[AsciiDoc user manual] ** 17.2. Metadata ** 17.3. Delimited blocks * 18. Paragraph +** 18.1. Alignment +** 18.2. Line breaks (" +\n") * 19. Text formatting ** 19.1. Bold and italic ** 19.2. Quotation Marks and Apostrophes @@ -113,8 +115,9 @@ List of features which will need to be implemented, * 16. Sections ** 16.9. Section styles * 18. Paragraph -** 18.1. Alignment ** 18.2. Line breaks +*** Per block "[%hardbreaks]" +*** All document ":hardbreaks:" ** 18.3. Lead style * 19. Text formatting ** 19.5. Custom Styling With Attributes diff --git a/adoc_node.go b/adoc_node.go index fb55f45..263843e 100644 --- a/adoc_node.go +++ b/adoc_node.go @@ -266,6 +266,18 @@ func (node *adocNode) addChild(child *adocNode) { } } +// backTrimSpace remove trailing white spaces on raw field. +func (node *adocNode) backTrimSpace() { + x := len(node.raw) - 1 + for ; x > 0; x-- { + if ascii.IsSpace(node.raw[x]) { + continue + } + break + } + node.raw = node.raw[:x+1] +} + func (node *adocNode) debug(n int) { for x := 0; x < n; x++ { fmt.Printf("\t") diff --git a/parser_inline.go b/parser_inline.go index 293c176..b37d84f 100644 --- a/parser_inline.go +++ b/parser_inline.go @@ -34,7 +34,7 @@ func newParserInline(doc *Document, content []byte) (pi *parserInline) { container: &adocNode{ kind: nodeKindText, }, - content: bytes.TrimRight(content, "\n"), + content: content, doc: doc, state: &parserInlineState{}, } @@ -83,6 +83,13 @@ func (pi *parserInline) do() { continue } } + if pi.prev == ' ' && pi.nextc == '\n' { + pi.current.backTrimSpace() + pi.current.WriteString("<br>\n") + pi.x += 2 + pi.prev = 0 + continue + } if pi.parsePassthrough() { continue } @@ -361,6 +368,12 @@ func (pi *parserInline) do() { pi.prev = pi.c } + // Remove any trailing spaces only if the node is not passthrough. + if !(pi.current.kind == nodeKindPassthrough || + pi.current.kind == nodeKindPassthroughDouble || + pi.current.kind == nodeKindPassthroughTriple) { + pi.current.backTrimSpace() + } pi.container.removeLastIfEmpty() } @@ -465,6 +478,7 @@ func (pi *parserInline) parseInlineID() bool { ID: id, kind: nodeKindInlineID, } + pi.current.backTrimSpace() pi.current.addChild(node) node = &adocNode{ kind: nodeKindText, @@ -506,6 +520,7 @@ func (pi *parserInline) parseInlineIDShort() bool { kind: nodeKindInlineIDShort, } pi.state.push(nodeKindInlineIDShort) + pi.current.backTrimSpace() pi.current.addChild(node) pi.current = node pi.x += 2 + len(id) + 2 diff --git a/testdata/got.test.html b/testdata/got.test.html index fffb3a5..dacc40c 100644 --- a/testdata/got.test.html +++ b/testdata/got.test.html @@ -42,7 +42,12 @@ <li><a href="#_section_two">Section Two</a></li> <li><a href="#_section_three">4. Section Three</a></li> <li><a href="#_comment">5. Comment</a></li> -<li><a href="#_paragraph">6. Paragraph</a></li> +<li><a href="#_paragraph">6. Paragraph</a> +<ul class="sectlevel2"> +<li><a href="#_alignment">6.1. Alignment</a></li> +<li><a href="#_line_break">6.2. Line break</a></li> +</ul> +</li> <li><a href="#_unconstrained_text_formatting">7. Unconstrained text formatting</a></li> <li><a href="#_single_quote">8. Single quote</a></li> <li><a href="#_subscript_and_superscript">9. Subscript and superscript</a></li> @@ -74,9 +79,9 @@ <li><a href="#_urls">21. URLs</a></li> <li><a href="#_anchors">22. Anchors</a> <ul class="sectlevel2"> -<li><a href="#version-4_9">22.1. Version 4.9 </a></li> -<li><a href="#version-4_10">22.2. Version 4.10 </a></li> -<li><a href="#which-one">22.3. Version 4.11 </a></li> +<li><a href="#version-4_9">22.1. Version 4.9</a></li> +<li><a href="#version-4_10">22.2. Version 4.10</a></li> +<li><a href="#which-one">22.3. Version 4.11</a></li> </ul> </li> <li><a href="#_cross_references">23. Cross references</a> @@ -275,6 +280,42 @@ This is next line in paragraph.</p> <pre>This is literal</pre> </div> </div> +<div class="sect2"> +<h3 id="_alignment"><a class="anchor" href="#_alignment"></a><a class="link" href="#_alignment">6.1. Alignment</a></h3> +<div class="paragraph text-left"> +<p>This text is left aligned.</p> +</div> +<div class="paragraph text-right"> +<p>This text is right aligned.</p> +</div> +<div class="paragraph text-center"> +<p>This text is center aligned.</p> +</div> +<div class="paragraph text-justify"> +<p>This text is justify aligned.</p> +</div> +</div> +<div class="sect2"> +<h3 id="_line_break"><a class="anchor" href="#_line_break"></a><a class="link" href="#_line_break">6.2. Line break</a></h3> +<div class="paragraph"> +<p>Rubies are red,<br> +Topazes are blue.</p> +</div> +<div class="ulist"> +<ul> +<li> +<p>is this<br> +line break</p> +</li> +<li> +<p>too<br></p> +</li> +<li> +<p>or not</p> +</li> +</ul> +</div> +</div> </div> </div> <div class="sect1"> @@ -1302,7 +1343,7 @@ Hard drive <p><a href="https://asciidoctor.org" class="bare">https://asciidoctor.org</a>.</p> </div> <div class="paragraph"> -<p><a href="https://asciidoctor.org" class="b a" target="_blank" rel="noopener">Asciidoctor</a>.</p> +<p><a href="https://asciidoctor.org" class="a b" target="_blank" rel="noopener">Asciidoctor</a>.</p> </div> <div class="paragraph"> <p><a href="https://kilabit.info" target="_blank" rel="noopener">Kilabit</a>.</p> @@ -1353,13 +1394,13 @@ Hard drive </ul> </div> <div class="sect2"> -<h3 id="version-4_9"><a class="anchor" href="#version-4_9"></a><a class="link" href="#version-4_9">22.1. Version 4.9 </a></h3> +<h3 id="version-4_9"><a class="anchor" href="#version-4_9"></a><a class="link" href="#version-4_9">22.1. Version 4.9</a></h3> </div> <div class="sect2"> -<h3 id="version-4_10"><a class="anchor" href="#version-4_10"></a><a class="link" href="#version-4_10">22.2. <a id="current"></a>Version 4.10 </a></h3> +<h3 id="version-4_10"><a class="anchor" href="#version-4_10"></a><a class="link" href="#version-4_10">22.2. <a id="current"></a>Version 4.10</a></h3> </div> <div class="sect2"> -<h3 id="which-one"><a class="anchor" href="#which-one"></a><a class="link" href="#which-one">22.3. Version 4.11 <a id="version-4_11"></a></a></h3> +<h3 id="which-one"><a class="anchor" href="#which-one"></a><a class="link" href="#which-one">22.3. Version 4.11<a id="version-4_11"></a></a></h3> </div> </div> </div> @@ -1999,7 +2040,7 @@ this sidebar.</p> <div id="footer"> <div id="footer-text"> 1.1.1<br> -Last updated 2020-11-16 01:04:36 +0700 +Last updated 2020-11-16 01:17:55 +0700 </div> </div> </body> diff --git a/testdata/test.adoc b/testdata/test.adoc index a8097d0..c5689e9 100644 --- a/testdata/test.adoc +++ b/testdata/test.adoc @@ -119,6 +119,30 @@ A paragraph followed by literal named [literal] This is literal +=== Alignment + +[.text-left] +This text is left aligned. + +[.text-right] +This text is right aligned. + +[.text-center] +This text is center aligned. + +[.text-justify] +This text is justify aligned. + +=== Line break + +Rubies are red, + +Topazes are blue. + +* is this + +line break +* too + +* or not + == Unconstrained text formatting diff --git a/testdata/test.html b/testdata/test.html index f964a64..ac2c339 100644 --- a/testdata/test.html +++ b/testdata/test.html @@ -42,7 +42,12 @@ <li><a href="#_section_two">Section Two</a></li> <li><a href="#_section_three">4. Section Three</a></li> <li><a href="#_comment">5. Comment</a></li> -<li><a href="#_paragraph">6. Paragraph</a></li> +<li><a href="#_paragraph">6. Paragraph</a> +<ul class="sectlevel2"> +<li><a href="#_alignment">6.1. Alignment</a></li> +<li><a href="#_line_break">6.2. Line break</a></li> +</ul> +</li> <li><a href="#_unconstrained_text_formatting">7. Unconstrained text formatting</a></li> <li><a href="#_single_quote">8. Single quote</a></li> <li><a href="#_subscript_and_superscript">9. Subscript and superscript</a></li> @@ -277,6 +282,42 @@ This is next line in paragraph.</p> <pre>This is literal</pre> </div> </div> +<div class="sect2"> +<h3 id="_alignment"><a class="anchor" href="#_alignment"></a><a class="link" href="#_alignment">6.1. Alignment</a></h3> +<div class="paragraph text-left"> +<p>This text is left aligned.</p> +</div> +<div class="paragraph text-right"> +<p>This text is right aligned.</p> +</div> +<div class="paragraph text-center"> +<p>This text is center aligned.</p> +</div> +<div class="paragraph text-justify"> +<p>This text is justify aligned.</p> +</div> +</div> +<div class="sect2"> +<h3 id="_line_break"><a class="anchor" href="#_line_break"></a><a class="link" href="#_line_break">6.2. Line break</a></h3> +<div class="paragraph"> +<p>Rubies are red,<br> +Topazes are blue.</p> +</div> +<div class="ulist"> +<ul> +<li> +<p>is this<br> +line break</p> +</li> +<li> +<p>too<br></p> +</li> +<li> +<p>or not</p> +</li> +</ul> +</div> +</div> </div> </div> <div class="sect1"> @@ -1999,7 +2040,7 @@ this sidebar.</p> <div id="footer"> <div id="footer-text"> 1.1.1<br> -Last updated 2020-11-16 01:00:32 +0700 +Last updated 2020-11-16 01:17:55 +0700 </div> </div> </body> |
