Sha256: 0ba7ed73be0b5454881d01e31d5d10d1877791aec9de7dda21f37835690e4e2f

Contents?: true

Size: 1.88 KB

Versions: 9

Compression:

Stored size: 1.88 KB

Contents

require "date"
require "nokogiri"
require "htmlentities"
require "json"
require "pathname"
require "open-uri"
require "pp"

module Asciidoctor
  module Standoc
    module Cleanup
      # include footnotes inside figure
      def figure_footnote_cleanup(xmldoc)
        nomatches = false
        until nomatches
          q = "//figure/following-sibling::*[1][self::p and *[1][self::fn]]"
          nomatches = true
          xmldoc.xpath(q).each do |s|
            s.previous_element << s.first_element_child.remove
            s.remove
            nomatches = false
          end
        end
      end

      def table_footnote_renumber1(fn, i, seen)
        if seen[fn.text] then outnum = seen[fn.text]
        else
          i += 1
          outnum = i
          seen[fn.text] = outnum
        end
        fn["reference"] = (outnum - 1 + "a".ord).chr
        fn["table"] = true
        [i, seen]
      end

      def table_footnote_renumber(xmldoc)
        xmldoc.xpath("//table | //figure").each do |t|
          seen = {}
          i = 0
          t.xpath(".//fn").each do |fn|
            i, seen = table_footnote_renumber1(fn, i, seen)
          end
        end
      end

      def other_footnote_renumber1(fn, i, seen)
        unless fn["table"]
          if seen[fn.text] then outnum = seen[fn.text]
          else
            i += 1
            outnum = i
            seen[fn.text] = outnum
          end
          fn["reference"] = outnum.to_s
        end
        [i, seen]
      end

      def other_footnote_renumber(xmldoc)
        seen = {}
        i = 0
        xmldoc.xpath("//fn").each do |fn|
          i, seen = other_footnote_renumber1(fn, i, seen)
        end
      end

      def footnote_cleanup(xmldoc)
        table_footnote_renumber(xmldoc)
        other_footnote_renumber(xmldoc)
        xmldoc.xpath("//fn").each do |fn|
          fn.delete("table")
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
metanorma-standoc-1.1.1 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.1.0 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.14 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.13 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.12 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.11 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.10 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.9 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.8 lib/asciidoctor/standoc/cleanup_footnotes.rb