Skip to content

Commit

Permalink
FIXED: Require automatic abbreviation search to only match abbreviati…
Browse files Browse the repository at this point in the history
…ons > 1 character long, and only if they are a complete word
  • Loading branch information
fletcher committed Nov 18, 2023
1 parent b0b8521 commit cd8d3b0
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 8 deletions.
28 changes: 21 additions & 7 deletions src/writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1830,15 +1830,23 @@ void automatic_search_text(mmd_engine * e, token * t, trie * ac) {

token * tok = t;

char * source = e->dstr->str;

if (m) {
walker = m->next;

while (walker) {
token_split(tok, walker->start, walker->len, walker->match_type);

// Advance token to next token
while (tok && (tok->start < walker->start + walker->len)) {
tok = tok->next;
// Only if we match a full word
if (
(walker->start + walker->len == tok->start + tok->len) ||
(char_is_whitespace_or_line_ending_or_punctuation(source[walker->start + walker->len]))
) {
token_split(tok, walker->start, walker->len, walker->match_type);

// Advance token to next token
while (tok && (tok->start < walker->start + walker->len)) {
tok = tok->next;
}
}

// Advance to next match (if present)
Expand Down Expand Up @@ -1912,13 +1920,19 @@ void identify_global_search_terms(mmd_engine * e, scratch_pad * scratch) {
// Add abbreviations to search trie
for (int i = 0; i < e->abbreviation_stack->size; ++i) {
f = stack_peek_index(e->abbreviation_stack, i);
trie_insert(ac, f->label_text, PAIR_BRACKET_ABBREVIATION);

if (f->label_text && strlen(f->label_text) > 1) {
trie_insert(ac, f->label_text, PAIR_BRACKET_ABBREVIATION);
}
}

// Add glossary to search trie (without leading '?')
for (int i = 0; i < e->glossary_stack->size; ++i) {
f = stack_peek_index(e->glossary_stack, i);
trie_insert(ac, f->clean_text, PAIR_BRACKET_GLOSSARY);

if (f->clean_text && strlen(f->clean_text) > 1) {
trie_insert(ac, f->clean_text, PAIR_BRACKET_GLOSSARY);
}
}

ac_trie_prepare(ac);
Expand Down
4 changes: 4 additions & 0 deletions tests/MMD6Tests/Abbreviations.fodt
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ foo</text:p></text:list-item>
<text:p text:style-name="Standard">foo</text:p></text:list-item>

</text:list>

<text:p text:style-name="Standard">Abbr (Ab) Abbr (A) [>a].</text:p>

<text:p text:style-name="Standard">Abc abc Ab.</text:p>
</office:text>
</office:body>
</office:document>
4 changes: 4 additions & 0 deletions tests/MMD6Tests/Abbreviations.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ <h1 id="foobar">foo bar</h1>
<li><abbr title="FOO">foo</abbr></li>
</ul>

<p>Abbr (<abbr title="Abbr">Ab</abbr>) Abbr (<abbr title="Abbr">A</abbr>) [>a].</p>

<p>Abc abc <abbr title="Abbr">Ab</abbr>.</p>

<div class="footnotes">
<hr />
<ol>
Expand Down
4 changes: 4 additions & 0 deletions tests/MMD6Tests/Abbreviations.htmlc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ bar</p>
<li>foo</li>
</ul>

<p>Ab <a href="Abbr">>A</a> <a href="Abbr">>a</a>.</p>

<p>Abc abc Ab.</p>

<p>[>foo bar]: FOO BAR</p>

<p>[^note]: foo and bar</p>
2 changes: 1 addition & 1 deletion tests/MMD6Tests/Abbreviations.opml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<outline text="&gt;&gt;Preamble&lt;&lt;" _note="&#10;foo foo&#10;&#10;f.o.o. f.o.o.&#10;&#10;f-o-o f-o-o&#10;&#10;f o o f o o&#10;&#10;fo&apos;o fo&apos;o&#10;&#10;5&#10;&#10;[&gt;bar] [&gt;bar]&#10;&#10;[&gt;b.a.r.] [&gt;b.a.r.]&#10;&#10;[&gt;b-a-r] [&gt;b-a-r]&#10;&#10;[&gt;b a r] [&gt;b a r]&#10;&#10;[&gt;ba&apos;r] [&gt;ba&apos;r]&#10;&#10;10&#10;&#10;foo bar&#10;&#10;foo&#10;bar&#10;&#10;foo bar&#10;&#10;[&gt;(baz) BAZ]&#10;&#10;&gt; foo bar&#10;&#10;15&#10;&#10;| foo | bar |&#10;| --- | --- |&#10;| foo | bar |&#10;[foo bar]&#10;&#10;"></outline>
<outline text="foo bar" _note="&#10;"><outline text="foo bar" _note="&#10;"></outline>
</outline>
<outline text="foo bar" _note="&#10;![foo bar](http://foobar.net/)&#10;&#10;20&#10;&#10;[^foo and bar]&#10;&#10;[^note]&#10;&#10;* foo&#10;* foo&#10;* foo&#10;&#10;&#10;[&gt;foo]: FOO&#10;[&gt;f.o.o.]: F.O.O.&#10;[&gt;f-o-o]: F-O-O&#10;[&gt;f o o]: F O O&#10;[&gt;fo&apos;o]: FO&apos;O&#10;&#10;[&gt;bar]: BAR&#10;[&gt;b.a.r.]: B.A.R.&#10;[&gt;b-a-r]: B-A-R&#10;[&gt;b a r]: B A R&#10;[&gt;ba&apos;r]: BA&apos;R&#10;&#10;[&gt;foo bar]: FOO BAR&#10;&#10;[^note]: foo and bar&#10;"></outline>
<outline text="foo bar" _note="&#10;![foo bar](http://foobar.net/)&#10;&#10;20&#10;&#10;[^foo and bar]&#10;&#10;[^note]&#10;&#10;* foo&#10;* foo&#10;* foo&#10;&#10;Ab [&gt;A] [&gt;a].&#10;&#10;Abc abc Ab.&#10;&#10;[&gt;foo]: FOO&#10;[&gt;f.o.o.]: F.O.O.&#10;[&gt;f-o-o]: F-O-O&#10;[&gt;f o o]: F O O&#10;[&gt;fo&apos;o]: FO&apos;O&#10;&#10;[&gt;bar]: BAR&#10;[&gt;b.a.r.]: B.A.R.&#10;[&gt;b-a-r]: B-A-R&#10;[&gt;b a r]: B A R&#10;[&gt;ba&apos;r]: BA&apos;R&#10;&#10;[&gt;foo bar]: FOO BAR&#10;&#10;[^note]: foo and bar&#10;&#10;[&gt;A]: Abbr&#10;[&gt;Ab]: Abbr&#10;"></outline>
<outline text="&gt;&gt;Metadata&lt;&lt;">
<outline text="title" _note="Abbreviations"/>
<outline text="latexconfig" _note="article"/>
Expand Down
8 changes: 8 additions & 0 deletions tests/MMD6Tests/Abbreviations.tex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

\newacronym{foo bar}{foo bar}{FOO BAR}

\newacronym{A}{A}{Abbr}

\newacronym{Ab}{Ab}{Abbr}

\input{mmd6-article-begin}

\gls{foo} \gls{foo}
Expand Down Expand Up @@ -111,5 +115,9 @@ \part{foo bar}

\end{itemize}

\gls{Ab} \gls{A} {[>a]}.

Abc abc \gls{Ab}.

\input{mmd6-article-footer}
\end{document}
6 changes: 6 additions & 0 deletions tests/MMD6Tests/Abbreviations.text
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ foo bar
* foo
* foo

Ab [>A] [>a].

Abc abc Ab.

[>foo]: FOO
[>f.o.o.]: F.O.O.
Expand All @@ -78,3 +81,6 @@ foo bar
[>foo bar]: FOO BAR

[^note]: foo and bar

[>A]: Abbr
[>Ab]: Abbr

0 comments on commit cd8d3b0

Please sign in to comment.