Skip to content

Commit

Permalink
Don't emit superfluous newlines in -thtml.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristapsdz committed May 19, 2023
1 parent 4ed5239 commit f42602c
Show file tree
Hide file tree
Showing 36 changed files with 13 additions and 105 deletions.
18 changes: 9 additions & 9 deletions html.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ rndr_blockcode(struct lowdown_buf *ob,
const struct rndr_blockcode *parm,
const struct html *st)
{
if (ob->size && !hbuf_putc(ob, '\n'))
if (!newline(ob))
return 0;

if (parm->lang.size) {
Expand Down Expand Up @@ -220,7 +220,7 @@ rndr_definition(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{

if (ob->size && !hbuf_putc(ob, '\n'))
if (!newline(ob))
return 0;
if (!HBUF_PUTSL(ob, "<dl>\n"))
return 0;
Expand Down Expand Up @@ -329,7 +329,7 @@ rndr_header(struct lowdown_buf *ob, const struct lowdown_buf *content,
else if (level > 6)
level = 6;

if (ob->size && !hbuf_putc(ob, '\n'))
if (!newline(ob))
return 0;
if (!hbuf_printf(ob, "<h%zu", level))
return 0;
Expand Down Expand Up @@ -414,7 +414,7 @@ rndr_list(struct lowdown_buf *ob,
const struct rndr_list *param)
{

if (ob->size && !hbuf_putc(ob, '\n'))
if (!newline(ob))
return 0;
if (param->flags & HLIST_FL_ORDERED) {
if (param->start > 1) {
Expand Down Expand Up @@ -586,7 +586,7 @@ rndr_raw_block(struct lowdown_buf *ob,
if (org >= sz)
return 1;

if (ob->size && !hbuf_putc(ob, '\n'))
if (!newline(ob))
return 0;

if (!hbuf_put(ob, param->text.data + org, sz - org))
Expand All @@ -610,7 +610,7 @@ static int
rndr_hrule(struct lowdown_buf *ob)
{

if (ob->size && !hbuf_putc(ob, '\n'))
if (!newline(ob))
return 0;
return hbuf_puts(ob, "<hr/>\n");
}
Expand Down Expand Up @@ -707,7 +707,7 @@ rndr_table(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{

if (ob->size && !hbuf_putc(ob, '\n'))
if (!newline(ob))
return 0;
if (!HBUF_PUTSL(ob, "<table>\n"))
return 0;
Expand All @@ -721,7 +721,7 @@ rndr_table_header(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{

if (ob->size && !hbuf_putc(ob, '\n'))
if (!newline(ob))
return 0;
if (!HBUF_PUTSL(ob, "<thead>\n"))
return 0;
Expand Down Expand Up @@ -923,7 +923,7 @@ rndr_doc_footer(struct lowdown_buf *ob, const struct html *st)
*/

if (st->footsz > 0) {
if (ob->size && !hbuf_putc(ob, '\n'))
if (!newline(ob))
return 0;
if (!HBUF_PUTSL(ob,
"<div class=\"footnotes\">\n<hr/>\n<ol>\n"))
Expand Down
1 change: 0 additions & 1 deletion regress/bang-before-footnote.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<p>This is a test!<sup id="fnref1"><a href="#fn1" rel="footnote">1</a></sup></p>

<div class="footnotes">
<hr/>
<ol>
Expand Down
20 changes: 0 additions & 20 deletions regress/diff/diff.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<title>Lowdown Diffing Engine </title>
</head>
<body>

<h1 id="lowdown-diffing-engine">Lowdown Diffing Engine</h1>
<p>In this paper, I briefly describe the &#8220;diff&#8221; engine used in
<a href="https://kristaps.bsd.lv/lowdown/lowdown.1.html">lowdown-diff(1)</a> tool
Expand All @@ -37,18 +36,15 @@ <h1 id="lowdown-diffing-engine">Lowdown Diffing Engine</h1>
(or </ins><ins><a href="https://kristaps.bsd.lv/lowdown/diff.diff.pdf">diff.diff.pdf</a></ins><ins>), which
shows the difference between this document and a [fabricated] earlier
version.</ins><del>.</del></p>

<h2 id="introduction">Introduction</h2>
<p>Let two source files, <del><em>foo.md</em></del><ins><em>old.md</em></ins> and <del><em>bar.md</em></del><ins><em>new.md</em></ins>, refer to the old and new versions of a file respectively.<ins>The</ins> <ins>goal</ins> <ins>is</ins> <ins>to</ins> <ins>establish</ins> <ins>the</ins> <ins>changes</ins> <ins>between</ins> <ins>these</ins> <ins>snippets</ins> <ins>in</ins> <ins>formatted</ins> <ins>output.</ins> <ins>Let</ins>&#8217;s <ins>begin</ins> <ins>with</ins> <ins>the</ins> <ins>old</ins> <ins>version,</ins> <ins><em>old.md</em></ins><ins>.</ins></p>

<pre><code class="language-markdown">*Lorem* ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut [labore](index.html) et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut _aliquip_ ex ea commodo consequat. Duis aute irure dolor
in reprehenderit...
</code></pre>
<p>In the new version, <em>new.md</em>, I add some more links and styles.</p>

<pre><code class="language-markdown">*Lorem* ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut [labore](index.html) et dolore [magna
aliqua](index.html). Ut enim ad minim veniam, quis nostrud exercitation
Expand All @@ -58,7 +54,6 @@ <h2 id="introduction">Introduction</h2>
<p>The most simple way of viewing changes is with the venerable
<a href="https://man.openbsd.org/diff.1">diff(1)</a> utility. However, this will
only reflect changes in the input document&#8212;not the formatted output.</p>

<pre><code class="language-diff">--- old.md Tue Oct 17 11:25:01 2017
+++ new.md Tue Oct 17 11:25:01 2017
@@ -1,5 +1,5 @@
Expand All @@ -80,7 +75,6 @@ <h2 id="introduction">Introduction</h2>
<p>A similar possibility is to use
<a href="https://www.gnu.org/software/wdiff/">wdiff(1)</a>, which produces a set of
word-by-word differences.</p>

<pre><code class="language-markdown">*Lorem* ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut [labore](index.html) et dolore [-magna
aliqua.-] {+[magna aliqua](index.html).+} Ut enim ad minim veniam, quis
Expand Down Expand Up @@ -120,7 +114,6 @@ <h2 id="introduction">Introduction</h2>
the usual linear difference, as in the case of
<a href="https://man.openbsd.org/diff.1">diff(1)</a> and friends), one can work
within the language to produce differences.<del><sup id="fnref1"><a href="#fn1" rel="footnote">1</a></sup></del></p>

<h2 id="algorithm">Algorithm</h2>
<p>The algorithm is in effect an ordered tree diff. I began with
well-studied algorithms for a well-studied problem: XML tree
Expand All @@ -135,7 +128,6 @@ <h2 id="algorithm">Algorithm</h2>
in different ways, or augment it at a later date.</p>
<p>The BULD algorithm described in this paper is straightforward. It
begins with a short sanitisation pass.</p>

<ol>
<li><p>Annotate each node in the parse tree with a hash of the subtree
rooted at the node, inclusive.
Expand All @@ -149,7 +141,6 @@ <h2 id="algorithm">Algorithm</h2>
weight. Then, while the priority queue is non-empty:
(<a href="https://github.com/kristapsdz/lowdown/blob/master/diff.c">diff.c</a>,
<code>lowdown_diff()</code>)</p>

<ol>
<li>Pop the first node of the priority queue.</li>
<li>Look for candidates in the old document whose hash matches the
Expand Down Expand Up @@ -177,14 +168,12 @@ <h2 id="algorithm">Algorithm</h2>
</ol>
<p>My implementation changes or extends the BULD algorithm in several small
ways, described in the per-step documentation below.</p>

<h3 id="sanitise">Sanitise</h3>
<p>Before the BULD algorithm is run, the input tree is sanitised. This
process merges all adjacent text nodes into a single text node. By
doing so, possible differences are pushed into large blocks of
contiguous text&#8212;which in this case are managed by the word-difference
algorithm described later in this paper.</p>

<h3 id="annotation">Annotation</h3>
<p>Each node in the tree is annotated with a hash and a weight. The hash,
MD5, is computed in all data concerning a node. For example, normal
Expand All @@ -199,7 +188,6 @@ <h3 id="annotation">Annotation</h3>
<p>Non-leaf nodes compute their hashes from the node type and the hashes of
all of their children. Thus, this step is a bottom-up search.</p>
<p>Node weight is computed exactly as noted in the paper.</p>

<h3 id="optimal-candidacy">Optimal candidacy</h3>
<p>A node&#8217;s candidate in the old tree is one whose hash matches. In most
documents, there are many candidates for certain types of nodes.
Expand All @@ -211,7 +199,6 @@ <h3 id="optimal-candidacy">Optimal candidacy</h3>
<p>In the event of similar optimality, the node &#8220;closest&#8221; to the current
node is chosen. Proximity is defined by the node identifier, which is
its prefix order in the parse tree.</p>

<h3 id="propagate-up">&#8220;Propagate up&#8221;</h3>
<p>When propagating a match upward, the distance upward is bound depending
on the matched sub-tree as defined in the paper. This makes it so that
Expand All @@ -221,19 +208,16 @@ <h3 id="propagate-up">&#8220;Propagate up&#8221;</h3>
<p>I did modify the algorithm to propagate upward &#8220;for free&#8221; through
similar singleton nodes, even if it means going beyond the maximum
number allowed by the sub-tree weight.</p>

<h3 id="optimisation">Optimisation</h3>
<p>The <a href="https://kristaps.bsd.lv/lowdown/lowdown.1.html">lowdown-diff(1)</a>
algorithm has two optimisations, both lightly derived from the paper:
top-down and bottom-up propagation.</p>

<h4 id="top-down">Top-down</h4>
<p>The top-down optimisation, which is performed first, takes matched nodes and matches un-matched, non-terminal children by label.<ins>The</ins> <ins>children</ins> <ins>examined</ins> <ins>must</ins> <ins>be</ins> <ins>siblings</ins> <ins>of</ins> <ins>adjacent</ins> <ins>matching</ins> <ins>nodes.</ins></p>
<p>This is useful when, say, a document consists of several paragraphs
where the text has changed within paragraphs. It won&#8217;t be able to match
the text content, but it will match the paragraphs, which will push the
difference downward in the tree.</p>

<h4 id="bottom-up">Bottom-up</h4>
<p>In the bottom-up propagation, the weight of any given sub-tree is used
to compute how high a match will propagate. I extend the paper&#8217;s
Expand All @@ -246,12 +230,10 @@ <h4 id="bottom-up">Bottom-up</h4>
(where the parent node is equal in label and attributes to the examined
node) are computed. If any given parent of the matched children has
greater than 50% of the possible weight, it is matched.</p>

<h3 id="merging">Merging</h3>
<p>The merging phase, which is not described in the paper, is very
straightforward. It uses a recursive merge algorithm starting at the
root node of the new tree and the root node of the old tree.</p>

<ol>
<li>The invariant is that the current node is matched by the corresponding
node in the old tree.</li>
Expand Down Expand Up @@ -315,7 +297,6 @@ <h2 id="api">API</h2>
produces the merged tree.</p>
<p>A set of convenience functions, <code>lowdown_buf_diff()</code> and
<code>lowdown_file_diff()</code>, also provide this functionality.</p>

<h2 id="future-work">Future work</h2>
<p>There are many possible improvements to the algorithm.</p>
<del>
Expand All @@ -335,7 +316,6 @@ <h2 id="future-work">Future work</h2>
the insert&#47;delete macros don&#8217;t disrupt the flow of text.</p>
</del>
<p>Document last updated: $Date$</p>

<div class="footnotes">
<hr/>
<ol>
Expand Down
1 change: 0 additions & 1 deletion regress/diff/metadata-add.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<title></title>
</head>
<body>

<h1 id="section">section</h1>
<p>body</p>
</body>
Expand Down
1 change: 0 additions & 1 deletion regress/diff/metadata-change.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<title></title>
</head>
<body>

<h1 id="section">section</h1>
<p>body</p>
</body>
Expand Down
1 change: 0 additions & 1 deletion regress/diff/metadata-remove.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<title></title>
</head>
<body>

<h1 id="section">section</h1>
<p>body</p>
</body>
Expand Down
1 change: 0 additions & 1 deletion regress/diff/table-badfree.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ <h1 id="section">section</h1>
</del><del>
<ul>
<li><p>what</p>

<ul>
<li>what <strong>what</strong>?</li>
</ul></li>
Expand Down
3 changes: 0 additions & 3 deletions regress/footnote-in-table.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<p>first<sup id="fnref1"><a href="#fn1" rel="footnote">1</a></sup></p>

<table>
<thead>
<tr>
Expand All @@ -15,7 +14,6 @@
</tbody>
</table>
<p>Now<sup id="fnref4"><a href="#fn4" rel="footnote">4</a></sup></p>

<div class="footnotes">
<hr/>
<ol>
Expand All @@ -31,7 +29,6 @@
<li id="fn3">
<p>three&#160;<a href="#fnref3" rev="footnote">&#8617;</a></p>
<p>hello</p>

<ul>
<li>world</li>
</ul>
Expand Down
1 change: 0 additions & 1 deletion regress/footnote-multi.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<p>Hi.<sup id="fnref1"><a href="#fn1" rel="footnote">1</a></sup></p>
<p>Another hi.[^pt]</p>

<div class="footnotes">
<hr/>
<ol>
Expand Down
1 change: 0 additions & 1 deletion regress/footnote-nested.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<p>Hi.<sup id="fnref1"><a href="#fn1" rel="footnote">1</a></sup></p>

<div class="footnotes">
<hr/>
<ol>
Expand Down
1 change: 0 additions & 1 deletion regress/footnote.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<p>Hi.<sup id="fnref1"><a href="#fn1" rel="footnote">1</a></sup></p>

<div class="footnotes">
<hr/>
<ol>
Expand Down
1 change: 0 additions & 1 deletion regress/header-attr-class-multi.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<h1 id="a" class="b c">a</h1>
<p>c</p>

<h1 id="a-1" class="b e">a</h1>
<p>c</p>
1 change: 0 additions & 1 deletion regress/header-attr-class.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<h1 id="a" class="b">a</h1>
<p>c</p>

<h1 id="a-1" class="b">a</h1>
<p>c</p>
1 change: 0 additions & 1 deletion regress/header-attr-id-empty.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<h1 id="b"></h1>
<p>c</p>

<h1 id="b"></h1>
<p>c</p>
1 change: 0 additions & 1 deletion regress/header-attr-id-zerolen.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<h1 id="a">a</h1>
<p>c</p>

<h1 id="b">b</h1>
<p>c</p>
1 change: 0 additions & 1 deletion regress/header-attr-id.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<h1 id="b">a</h1>
<p>c</p>

<h1 id="b">a</h1>
<p>c</p>
1 change: 0 additions & 1 deletion regress/header-attr-id2.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<h1 id="wwaaww">a</h1>
<p>c</p>

<h1 id="www">a</h1>
<p>c</p>
9 changes: 1 addition & 8 deletions regress/header-ids.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
<h1 id="section">section</h1>

<h1 id="section-2">section-2</h1>

<h1 id="section-1">section</h1>

<h1 id="section-3">section</h1>

<h1 id="section-3-1">section-3</h1>

<h1 id="section-3-2">section 3</h1>

<h1 id="section3">section()3</h1>

<h1 id="section-3-3">section () 3</h1>
<h1 id="section-3-3">section () 3</h1>
1 change: 0 additions & 1 deletion regress/header-with-self-link.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<h1 id="header-with-link">Header with <a href="#link">link</a></h1>

<h1 id="link"><a href="#link">link</a></h1>
3 changes: 0 additions & 3 deletions regress/list-complex.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<p>An initial paragraph.</p>

<ul>
<li><p>An outer list.</p>
<p>With a paragraph.</p>

<ol>
<li>An inner ordered list.</li>
<li>That&#8217;s inline.</li>
<li>Followed by&#8230;</li>
</ol>

<ul>
<li><p>An inner regular lits.</p>
<p>With inner paragraph.</p></li>
Expand Down
1 change: 0 additions & 1 deletion regress/list-irregular-spacing.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<ul>
<li>One space,
with no indent for wrapped text.

<ol>
<li>Irregular nesting&#8230; DO NOT DO THIS.</li>
</ol></li>
Expand Down
1 change: 0 additions & 1 deletion regress/list-switch-listtype.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<ol>
<li>c</li>
</ol>

<ul>
<li> a</li>
</ul>
Loading

0 comments on commit f42602c

Please sign in to comment.