From 9b3c06d9647167c8b92caf118244dbdebd574e0c Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 28 Apr 2019 23:04:01 -0700 Subject: Fix tools/make_spec.lua so it properly handles cross-refs. Previously it broke in a few cases, e.g. with soft breaks. This fixes the issue described in #578. --- tools/make_spec.lua | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'tools') diff --git a/tools/make_spec.lua b/tools/make_spec.lua index 641bf58..05a1270 100644 --- a/tools/make_spec.lua +++ b/tools/make_spec.lua @@ -23,6 +23,18 @@ local render_number = function(tbl) return table.concat(buf, '.') end +local extract_label = function(cur) + local label = "" + for subcur, subentering, subnode_type in cmark.walk(cur) do + if subentering and subnode_type == cmark.NODE_TEXT then + label = label .. cmark.node_get_literal(subcur) + elseif subentering and subnode_type == cmark.NODE_SOFTBREAK then + label = label .. " " + end + end + return label +end + local extract_references = function(doc) local cur, entering, node_type local refs = {} @@ -30,8 +42,7 @@ local extract_references = function(doc) if not entering and ((node_type == cmark.NODE_LINK and cmark.node_get_url(cur) == '@') or node_type == cmark.NODE_HEADING) then - local child = cmark.node_first_child(cur) - local label = trim(cmark.render_commonmark(child, OPT_DEFAULT + OPT_UNSAFE, 0)) + local label = extract_label(cur) local ident = to_identifier(label) if refs[label] then warn("duplicate reference " .. label) @@ -118,8 +129,7 @@ local create_anchors = function(doc, meta, to) node_type == cmark.NODE_HEADING) then local anchor - local child = cmark.node_first_child(cur) - local label = trim(cmark.render_commonmark(child, OPT_DEFAULT + OPT_UNSAFE, 0)) + local label = extract_label(cur) local ident = to_identifier(label) if node_type == cmark.NODE_LINK then if format == 'latex' then @@ -167,7 +177,8 @@ local create_anchors = function(doc, meta, to) end end end - local children = {}; + local children = {} + local child = cmark.node_first_child(cur) while child do children[#children + 1] = child child = cmark.node_next(child) -- cgit v1.2.3