Skip to content

Commit

Permalink
Fix tools/make_spec.lua so it properly handles cross-refs.
Browse files Browse the repository at this point in the history
Previously it broke in a few cases, e.g. with soft breaks.

This fixes the issue described in #578.
  • Loading branch information
jgm committed Apr 29, 2019
1 parent 026ca82 commit 9b3c06d
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions tools/make_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,26 @@ 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 = {}
for cur, entering, node_type in cmark.walk(doc) do
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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9b3c06d

Please sign in to comment.