Skip to content

Commit

Permalink
MD_FLAG_HARD_SOFT_BREAKS (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
l1mey112 authored Jan 8, 2024
1 parent 4e5a6e6 commit 6ef3be6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions md2html/md2html.1
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ Allow e-mail autolinks without "<", ">" and "mailto:"
.TP
.B --fpermissive-autolinks
Enable all 3 of the above permissive autolinks options
.TP
.B --fhard-soft-breaks
Force all soft breaks to act as hard breaks
.
.TP
.B --fno-indented-code
Expand Down
4 changes: 4 additions & 0 deletions md2html/md2html.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ static const CMDLINE_OPTION cmdline_options[] = {
{ 0, "flatex-math", 'L', 0 },
{ 0, "fpermissive-atx-headers", 'A', 0 },
{ 0, "fpermissive-autolinks", 'V', 0 },
{ 0, "fhard-soft-breaks", 'B', 0 },
{ 0, "fpermissive-email-autolinks", '@', 0 },
{ 0, "fpermissive-url-autolinks", 'U', 0 },
{ 0, "fpermissive-www-autolinks", '.', 0 },
Expand Down Expand Up @@ -264,6 +265,8 @@ usage(void)
" --fpermissive-autolinks\n"
" Same as --fpermissive-url-autolinks --fpermissive-www-autolinks\n"
" --fpermissive-email-autolinks\n"
" --fhard-soft-breaks\n"
" Force all soft breaks to act as hard breaks\n"
" --fstrikethrough Enable strike-through spans\n"
" --ftables Enable tables\n"
" --ftasklists Enable task lists\n"
Expand Down Expand Up @@ -335,6 +338,7 @@ cmdline_callback(int opt, char const* value, void* data)
case 'K': parser_flags |= MD_FLAG_WIKILINKS; break;
case 'X': parser_flags |= MD_FLAG_TASKLISTS; break;
case '_': parser_flags |= MD_FLAG_UNDERLINE; break;
case 'B': parser_flags |= MD_FLAG_HARD_SOFT_BREAKS; break;

default:
fprintf(stderr, "Illegal option: %s\n", value);
Expand Down
4 changes: 4 additions & 0 deletions scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ echo
echo "WWW autolinks extension:"
$PYTHON "$TEST_DIR/spec_tests.py" -s "$TEST_DIR/permissive-www-autolinks.txt" -p "$PROGRAM --fpermissive-www-autolinks"

echo
echo "Hard soft breaks extension:"
$PYTHON "$TEST_DIR/spec_tests.py" -s "$TEST_DIR/hard-soft-breaks.txt" -p "$PROGRAM --fhard-soft-breaks"

echo
echo "Tables extension:"
$PYTHON "$TEST_DIR/spec_tests.py" -s "$TEST_DIR/tables.txt" -p "$PROGRAM --ftables"
Expand Down
4 changes: 3 additions & 1 deletion src/md4c.c
Original file line number Diff line number Diff line change
Expand Up @@ -4415,7 +4415,9 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
MD_TEXTTYPE break_type = MD_TEXT_SOFTBR;

if(text_type == MD_TEXT_NORMAL) {
if(enforce_hardbreak)
if(ctx->parser.flags & MD_FLAG_HARD_SOFT_BREAKS)
break_type = MD_TEXT_BR;
else if(enforce_hardbreak)
break_type = MD_TEXT_BR;
else if((CH(line->end) == _T(' ') && CH(line->end+1) == _T(' ')))
break_type = MD_TEXT_BR;
Expand Down
1 change: 1 addition & 0 deletions src/md4c.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ typedef struct MD_SPAN_WIKILINK {
#define MD_FLAG_LATEXMATHSPANS 0x1000 /* Enable $ and $$ containing LaTeX equations. */
#define MD_FLAG_WIKILINKS 0x2000 /* Enable wiki links extension. */
#define MD_FLAG_UNDERLINE 0x4000 /* Enable underline extension (and disables '_' for normal emphasis). */
#define MD_FLAG_HARD_SOFT_BREAKS 0x8000 /* Force all soft breaks to act as hard breaks. */

#define MD_FLAG_PERMISSIVEAUTOLINKS (MD_FLAG_PERMISSIVEEMAILAUTOLINKS | MD_FLAG_PERMISSIVEURLAUTOLINKS | MD_FLAG_PERMISSIVEWWWAUTOLINKS)
#define MD_FLAG_NOHTML (MD_FLAG_NOHTMLBLOCKS | MD_FLAG_NOHTMLSPANS)
Expand Down
23 changes: 23 additions & 0 deletions test/hard-soft-breaks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

# Hard Soft Breaks

With the flag `MD_FLAG_HARD_SOFT_BREAKS`, MD4C treats all newline characters as soft breaks.

```````````````````````````````` example
foo
baz
.
<p>foo<br>
baz</p>
````````````````````````````````

```````````````````````````````` example
A quote from the CommonMark Spec below:

A renderer may also provide an option to
render soft line breaks as hard line breaks.
.
<p>A quote from the CommonMark Spec below:</p>
<p>A renderer may also provide an option to<br>
render soft line breaks as hard line breaks.</p>
````````````````````````````````

0 comments on commit 6ef3be6

Please sign in to comment.