Skip to content

Commit 9ddb6a1

Browse files
committed
The push() function now returns the index in the queue, rather than the pushed value, for a slightly shorter code.
1 parent 4a3e3f2 commit 9ddb6a1

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed

markdown.awk

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ function inline(s, no_links, replacements, result, t, i, x)
170170
t = t substr(s, 1, i - 1) result["html"]
171171
s = result["rest"]
172172
} else { # Apparently not a delimiter, take it literally
173-
push(replacements, esc_html(x[0]))
174-
t = t substr(s, 1, i - 1) "\002" size(replacements) "\003"
173+
t = t substr(s, 1, i - 1) "\002" push(replacements, esc_html(x[0])) "\003"
175174
s = substr(s, i + length(x[0]))
176175
}
177176
s = t s
@@ -188,8 +187,7 @@ function inline(s, no_links, replacements, result, t, i, x)
188187
t = t substr(s, 1, i - 1) result["html"]
189188
s = result["rest"]
190189
} else { # Apparently not an opening delimiter, skip it
191-
push(replacements, x[0])
192-
t = t substr(s, 1, i - 1) "\002" size(replacements) "\003"
190+
t = t substr(s, 1, i - 1) "\002" push(replacements, x[0]) "\003"
193191
s = substr(s, i + length(x[0]))
194192
}
195193
s = t s
@@ -205,16 +203,14 @@ function inline(s, no_links, replacements, result, t, i, x)
205203
t = t substr(s, 1, i - 1) result["html"]
206204
s = result["rest"]
207205
} else { # Apparently not a valid delimiter. Skip it.
208-
push(replacements, x[0])
209-
t = t substr(s, 1, i - 1) "\002" size(replacements)"\003"
206+
t = t substr(s, 1, i - 1) "\002" push(replacements, x[0]) "\003"
210207
s = substr(s, i + length(x[0]))
211208
}
212209
s = t s
213210

214211
# Replace hard line breaks by <br> and remove backslash escapes.
215212
#
216-
push(replacements, "<br />\n")
217-
t = "\002" size(replacements) "\003"
213+
t = "\002" push(replacements, "<br />\n") "\003"
218214
s = awk::gensub(/ \n/, "\n", "g",
219215
awk::gensub(/( +|\\)\n/, t, "g", esc_html(unesc_md(s))))
220216

@@ -260,8 +256,7 @@ function inline_code_span(s, i, replacements, result,
260256

261257
# Store HTML code in replacements.
262258
t = "<code>" esc_html(content) "</code>"
263-
push(replacements, t)
264-
n = size(replacements)
259+
n = push(replacements, t)
265260

266261
# Return result.
267262
result["html"] = "\002" n "\003"
@@ -315,8 +310,7 @@ function inline_autolink(s, i, no_links, replacements, result,
315310
return 0
316311
}
317312

318-
push(replacements, t) # Store the HTML code in replacements
319-
n = size(replacements) # Get the index where it was stored
313+
n = push(replacements, t) # Store the HTML code in replacements, get its index
320314
result["html"] = "\002" n "\003" # The tag to replace the auutolink with
321315
result["rest"] = substr(s, length(x[0]) + 1)
322316
# print "Found autolink <" n "> = " t > "/dev/stderr"
@@ -356,8 +350,7 @@ function inline_html_tag(s, i, replacements, result,
356350
match(s, /^<![a-zA-Z][^>]*>/, x) ||
357351
match(s, /^<\?([^?>]|\?[^>])*\?>/, x)) {
358352
t = x[0] # Copy the tag verbatim
359-
push(replacements, t) # Store the HTML code in replacements
360-
n = size(replacements) # Get the index where it was stored
353+
n = push(replacements, t) # # Store HTML code in replacements, get its index
361354
result["html"] = "\002" n "\003" # The tag to replace the auutolink with
362355
result["rest"] = substr(s, length(x[0]) + 1)
363356
# print "Found HTML tag <" n "> = " t > "/dev/stderr"
@@ -414,8 +407,7 @@ function inline_link_or_image(s, i, no_links, replacements, result,
414407
inline_html_tag(s, j, replacements, result1)) {
415408
t = t substr(s, 1, j - 1) result1["html"]; s = result1["rest"]
416409
} else { # delimiter that does not start anything
417-
push(replacements, esc_html(x[0]))
418-
t = t substr(s, 1, i - 1) "\002" size(replacements) "\003"
410+
t = t substr(s, 1, i-1) "\002" push(replacements, esc_html(x[0])) "\003"
419411
s = substr(s, i + length(x[0]))
420412
}
421413
} else if (x[0] == "![") {
@@ -424,8 +416,7 @@ function inline_link_or_image(s, i, no_links, replacements, result,
424416
t = t substr(s, 1, j - 1) result1["html"]; s = result1["rest"]
425417
} else { # delimiter that does not start anything
426418
n++
427-
push(replacements, esc_html(x[0]))
428-
t = t substr(s, 1, i - 1) "\002" size(replacements) "\003"
419+
t = t substr(s, 1, i-1) "\002" push(replacements, esc_html(x[0])) "\003"
429420
s = substr(s, i + length(x[0]))
430421
}
431422
} else if (x[0] == "[") {
@@ -512,8 +503,7 @@ function inline_link_or_image(s, i, no_links, replacements, result,
512503

513504
# Store the HTML code in replacements at index n and return a tag
514505
# "<n>".
515-
push(replacements, u)
516-
result["html"] = "\002" size(replacements) "\003"
506+
result["html"] = "\002" push(replacements, u) "\003"
517507
result["rest"] = s
518508
# print "\"" u "\" -> " size(replacements) > "/dev/stderr"
519509
return 1
@@ -611,8 +601,7 @@ function inline_emphasis(s, i, no_links, replacements, result,
611601
if (j != 0) {
612602
# We found and processed an opening delimiter. Replace the
613603
# processed part in the string s by the result of processing.
614-
push(replacements, result1["html"])
615-
t = "\002" size(replacements) "\003"
604+
t = "\002" push(replacements, result1["html"]) "\003"
616605
result["html"] = x[1] substr(x[2], 1, j - 1) t
617606
result["rest"] = result1["rest"]
618607
s = result["html"] result["rest"]
@@ -632,16 +621,12 @@ function inline_emphasis(s, i, no_links, replacements, result,
632621
# how many *'s were matched (= n).
633622
n = min(length(x[1]), closinglen)
634623
for (j = n; j > 1; j -= 2) {
635-
push(replacements, "<strong>")
636-
t = "\002" size(replacements) "\003" t
637-
push(replacements, "</strong>")
638-
t = t "\002" size(replacements) "\003"
624+
t = "\002" push(replacements, "<strong>") "\003" t
625+
t = t "\002" push(replacements, "</strong>") "\003"
639626
}
640627
if (n % 2 == 1) {
641-
push(replacements, "<em>")
642-
t = "\002" size(replacements) "\003" t
643-
push(replacements, "</em>")
644-
t = t "\002" size(replacements) "\003"
628+
t = "\002" push(replacements, "<em>") "\003" t
629+
t = t "\002" push(replacements, "</em>") "\003"
645630
}
646631

647632
# The result so far consists of any remaining part of the
@@ -708,10 +693,8 @@ function inline_strikethrough(s, i, no_links, replacements, result,
708693
# Process the text between the two delimiters and enclose the result
709694
# in <del> and </del>.
710695
t = inline(substr(s, 1, j - 1), no_links, replacements)
711-
push(replacements, "<del>")
712-
t = "\002" size(replacements) "\003" t
713-
push(replacements, "</del>")
714-
t = t "\002" size(replacements) "\003"
696+
t = "\002" push(replacements, "<del>") "\003" t
697+
t = t "\002" push(replacements, "</del>") "\003"
715698

716699
result["html"] = t
717700
result["rest"] = substr(s, j + length(x[1]))
@@ -1475,7 +1458,7 @@ function esc_url(s)
14751458
# queue/stack is empty.
14761459

14771460

1478-
# push -- add a value to a stack or a queue, return the value
1461+
# push -- add a value to a stack or a queue, return its index
14791462
function push(stack, value)
14801463
{
14811464
# If stack is uninitialized, initialize it
@@ -1484,7 +1467,7 @@ function push(stack, value)
14841467
stack["last"] = 0
14851468
}
14861469
stack[++stack["last"]] = value
1487-
return value
1470+
return stack["last"]
14881471
}
14891472

14901473

@@ -1507,7 +1490,7 @@ function top(stack)
15071490
}
15081491

15091492

1510-
# shift -- add an item at the start of a queue, return the item
1493+
# shift -- add an item at the start of a queue, return its index
15111494
function shift(queue, value)
15121495
{
15131496
# If the queue is uninitialized, initialize it
@@ -1516,7 +1499,7 @@ function shift(queue, value)
15161499
queue["last"] = 0
15171500
}
15181501
queue[--queue["first"]] = value
1519-
return value
1502+
return queue["first"]
15201503
}
15211504

15221505

0 commit comments

Comments
 (0)