Commits

Taylor Venable  committed 73bf47c

Show first back-reference for each module.

  • Participants
  • Parent commits 02ff61f

Comments (0)

Files changed (1)

File tensile.tnsl

     local state = "doc"
     local code = nil
     local doc = nil
-    local ir = {src = {}, doc = {}}
+    local ir = {src = {}, doc = {}, ref = {}}
     local unit = {}
     local unitName = ""
     local function defineUnit(name)
                     if ref then
                         if code then unit[#unit + 1] = { type = "code", text = code } ; code = nil end
                         unit[#unit + 1] = { type = "ref", name = ref }
+
+                        local r1 = ir.ref[unitName]
+                        r1 = r1 or { fwd = {}, back = {} }
+                        r1.fwd[#r1.fwd + 1] = ref
+                        ir.ref[unitName] = r1
+
+                        local r2 = ir.ref[ref]
+                        r2 = r2 or { fwd = {}, back = {} }
+                        r2.back[#r2.back + 1] = unitName
+                        ir.ref[ref] = r2
                     else
                         code = (code and code .. "\n" or "") .. line
                     end
         end
         stream:write("    },\n")
     end
+    stream:write("  },\n")
+    stream:write("  ref = {\n")
+    for k,v in pairs(ir.ref) do
+        stream:write("    [\"" .. k .. "\"] = {\n")
+        stream:write("      fwd = {")
+        for i2,v2 in ipairs(ir.ref[k].fwd) do
+            stream:write("\"" .. v2 .. "\", ")
+        end
+        stream:write("},\n")
+        stream:write("      back = {")
+        for i2,v2 in ipairs(ir.ref[k].back) do
+            stream:write("\"" .. v2 .. "\", ")
+        end
+        stream:write("}\n")
+        stream:write("    },\n")
+    end
     stream:write("  }\n")
     stream:write("}\n")
     stream:close()
                 stream:write(tostring(sublabel))
                 stream:write("}}}")
             end
+
             stream:write("\\moddef{" .. v.name)
+
             if g_opts.show_defn_page then
                 stream:write("~{\\nwtagstyle{}\\subpageref{")
                 local s = tostring(sublabel)
                 s = v.start and s:gsub("-%d+$", "-0") or s
                 stream:write(s .. "}}")
             end
-            if v.start then
-                stream:write("}\\plusendmoddef\n")
-                GenerateDoc(ir.src[v.name], stream, v.start)
-            else
-                stream:write("}\\endmoddef\n")
-                GenerateDoc(ir.src[v.name], stream)
+
+            stream:write("}\\" .. (v.start and "plus" or "") .. "endmoddef")
+            stream:write("\\nwstartdeflinemarkup")
+
+            if g_opts.show_back_refs and #ir.ref[v.name].back > 0 then
+                stream:write("\\nwusesondefline{\\\\{")
+                stream:write(tostring(subcounter[ir.ref[v.name].back[1]]))
+                stream:write("}}")
+            end
+
+            stream:write("\\nwenddeflinemarkup")
+            stream:write("\n")
+            GenerateDoc(ir.src[v.name], stream, v.start)
+
+            if g_opts.show_back_refs and #ir.ref[v.name].back > 0 then
+                stream:write("\\nwused{\\\\{")
+                stream:write(tostring(subcounter[ir.ref[v.name].back[1]]))
+                stream:write("}}")
             end
             stream:write("\\nwendcode{}\n")
         end
 
 g_opts = {
     show_margin_tags = true,
-    show_defn_page = true
+    show_defn_page = true,
+    show_back_refs = true
 }
 
 for i,v in ipairs(arg) do