Sha256: c93d0bbfe45f1b27f7b56db083acd83a3b4f8ce93444606cfc4f2f1e4a2ceaa6

Contents?: true

Size: 1.88 KB

Versions: 10

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_renumber(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

10 entries across 10 versions & 1 rubygems

Version Path
metanorma-standoc-1.0.7 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.6 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.5 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.4 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.3 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.2 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.1 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-1.0.0 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-0.0.2 lib/asciidoctor/standoc/cleanup_footnotes.rb
metanorma-standoc-0.0.1 lib/asciidoctor/standoc/cleanup_footnotes.rb