Skip to content

Commit d0d780e

Browse files
committed
Markdown: Allow up to 3 whitespaces in front of first character like #,`,~,-,=
1 parent bd8d8c4 commit d0d780e

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

Units/parser-markdown.r/simple-markdown.d/expected.tags

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ F input.md /^F$/;" s chapter:C end:106 sectionMarker:-
2828
G input.md /^ G$/;" s chapter:C end:109 sectionMarker:-
2929
H input.md /^ H$/;" s chapter:C end:112 sectionMarker:-
3030
I input.md /^ I$/;" s chapter:C end:128 sectionMarker:-
31-
C\\# input.md /^# C\\#$/;" c end:129 sectionMarker:#
31+
C\\# input.md /^# C\\#$/;" c end:141 sectionMarker:#
32+
J input.md /^J$/;" s chapter:C\\# end:133 sectionMarker:-
33+
K input.md /^K$/;" s chapter:C\\# end:136 sectionMarker:-
34+
L input.md /^L$/;" s chapter:C\\# end:141 sectionMarker:-
3235
x input.md /^function x$/;" f
3336
y input.md /^function y$/;" f
3437
z input.md /^z()$/;" f

Units/parser-markdown.r/simple-markdown.d/input.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,15 @@ text
127127
-
128128

129129
# C\#
130+
131+
J
132+
-
133+
134+
K
135+
-
136+
137+
L
138+
-
139+
140+
ignored
141+
-

parsers/markdown.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,14 @@ static vString *get_heading(const int kind, const unsigned char *line,
162162
}
163163

164164

165-
static bool is_indented(const unsigned char *line, int line_len)
165+
static int get_first_char_pos(const unsigned char *line, int line_len, bool *indented)
166166
{
167167
int indent = 0;
168-
for (int i = 0; i < line_len && isspace(line[i]) && indent < 4; i++)
168+
int i;
169+
for (i = 0; i < line_len && isspace(line[i]); i++)
169170
indent += line[i] == '\t' ? 4 : 1;
170-
return indent >= 4;
171+
*indented = indent >= 4;
172+
return i;
171173
}
172174

173175

@@ -197,23 +199,25 @@ static void findMarkdownTags(void)
197199
{
198200
int line_len = strlen((const char*) line);
199201
bool line_processed = false;
202+
bool indented;
203+
int pos = get_first_char_pos(line, line_len, &indented);
200204

201-
for (int i = 0; i < 2; i++)
205+
for (int i = 0; i < 2 && !indented; i++)
202206
{
203207
char code_chars[] = { '`', '~' };
204208
char c = code_chars[i % 2];
205209
char other_c = code_chars[(i + 1) % 2];
206210

207211
if (in_code_char != other_c && line_len >= 3 &&
208-
line[0] == c && line[1] == c && line[2] == c)
212+
line[pos] == c && line[pos+1] == c && line[pos+2] == c)
209213
{
210214
in_code_char = in_code_char ? 0 : c;
211215
if (in_code_char)
212216
{
213217
startSourceLineNumber = getSourceLineNumber ();
214218
startLineNumber = getInputLineNumber ();
215219
vStringClear(codeLang);
216-
vStringCatS(codeLang, (const char *)(line + 3));
220+
vStringCatS(codeLang, (const char *)(line + pos + 3));
217221
vStringStripLeading(codeLang);
218222
vStringStripTrailing(codeLang);
219223
}
@@ -234,23 +238,23 @@ static void findMarkdownTags(void)
234238
line_processed = true;
235239

236240
/* code block using indent */
237-
else if (is_indented(line, line_len))
241+
else if (indented)
238242
line_processed = true;
239243

240244
/* if it's a title underline, or a delimited block marking character */
241-
else if (line[0] == '=' || line[0] == '-' || line[0] == '#')
245+
else if (line[pos] == '=' || line[pos] == '-' || line[pos] == '#')
242246
{
243247
int n_same;
244-
for (n_same = 1; line[n_same] == line[0]; ++n_same);
248+
for (n_same = 1; line[n_same] == line[pos]; ++n_same);
245249

246250
/* is it a two line title */
247-
if (line[0] == '=' || line[0] == '-')
251+
if (line[pos] == '=' || line[pos] == '-')
248252
{
249-
char marker[2] = { line[0], '\0' };
250-
int kind = line[0] == '=' ? K_CHAPTER : K_SECTION;
253+
char marker[2] = { line[pos], '\0' };
254+
int kind = line[pos] == '=' ? K_CHAPTER : K_SECTION;
251255
bool whitespace_terminated = true;
252256

253-
for (int i = n_same; i < line_len; i++)
257+
for (int i = pos + n_same; i < line_len; i++)
254258
{
255259
if (!isspace(line[i]))
256260
{
@@ -265,7 +269,7 @@ static void findMarkdownTags(void)
265269
makeSectionMarkdownTag(prev_line, kind, marker);
266270
}
267271
/* otherwise is it a one line title */
268-
else if (line[0] == '#' && n_same <= K_SECTION_COUNT && isspace(line[n_same]))
272+
else if (line[pos] == '#' && n_same <= K_SECTION_COUNT && isspace(line[n_same]))
269273
{
270274
int kind = n_same - 1;
271275
bool delimited = false;

0 commit comments

Comments
 (0)