From 27a47c28203d18d3f0fbf59b40c7ee7c838b638c Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Thu, 24 Oct 2024 22:40:25 +1100 Subject: [PATCH] list numbering in Japanese numbers: https://github.com/metanorma/metanorma-jis/issues/228 --- lib/isodoc/jis/xref.rb | 42 +++++++++++++++++-------- spec/isodoc/blocks_spec.rb | 63 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 12 deletions(-) diff --git a/lib/isodoc/jis/xref.rb b/lib/isodoc/jis/xref.rb index 73f7596..948baa8 100644 --- a/lib/isodoc/jis/xref.rb +++ b/lib/isodoc/jis/xref.rb @@ -5,13 +5,18 @@ def ol_type(list, depth) return list["type"].to_sym if list["type"] return :alphabet if depth == 1 - :arabic + @style == :japanese ? :japanese : :arabic end def listlabel(_list, depth) case depth when 1 then (96 + @num).chr.to_s - else @num.to_s + else + if @style == :japanese + @num.localize(:ja).spellout + else + @num.to_s + end end end end @@ -19,13 +24,22 @@ def listlabel(_list, depth) class Xref < IsoDoc::Iso::Xref attr_accessor :autonumbering_style + def clause_sep + @autonumbering_style == :japanese ? "\u30fb" : "." + end + def clause_counter(num, opts) opts[:numerals] ||= @autonumbering_style - opts[:separator] ||= - @autonumbering_style == :japanese ? "・" : "." + opts[:separator] ||= clause_sep super end + def list_counter(num, opts) + opts[:numerals] ||= @autonumbering_style + opts[:separator] ||= clause_sep + IsoDoc::Jis::Counter.new(num, opts) + end + def hierfigsep @lang == "ja" ? "の" : super end @@ -140,15 +154,19 @@ def commentary_name_anchors(clause, num, root, level) def list_item_anchor_names(list, list_anchor, depth, prev_label, refer_list) - c = Counter.new(list["start"] ? list["start"].to_i - 1 : 0) + c = list_counter(list["start"] ? list["start"].to_i - 1 : 0, {}) list.xpath(ns("./li")).each do |li| bare_label, label = - list_item_value(li, c, depth, { list_anchor: list_anchor, - prev_label: prev_label, refer_list: depth == 1 ? refer_list : nil }) + list_item_value(li, c, depth, + { list_anchor: list_anchor, + prev_label: prev_label, + refer_list: depth == 1 ? refer_list : nil }) li["id"] and @anchors[li["id"]] = - { label: bare_label, bare_xref: "#{bare_label})", + { label: bare_label, + bare_xref: "#{bare_label})", xref: "#{label})", type: "listitem", - refer_list: refer_list, container: list_anchor[:container] } + refer_list: refer_list, + container: list_anchor[:container] } (li.xpath(ns(".//ol")) - li.xpath(ns(".//ol//ol"))).each do |ol| list_item_anchor_names(ol, list_anchor, depth + 1, label, refer_list) @@ -159,10 +177,10 @@ def list_item_anchor_names(list, list_anchor, depth, prev_label, def list_item_value(entry, counter, depth, opts) label1 = counter.increment(entry).listlabel(entry.parent, depth) if depth > 2 - base = opts[:prev_label].match(/^(.*?)([0-9.]+)$/) # a) 1.1.1 - label1 = "#{base[2]}.#{label1}" + base = @c.decode(opts[:prev_label]).split(/\)/) # List a) 1.1.1 + label1 = "#{base[-1].sub(/^の/,'')}#{clause_sep}#{label1}" [label1, list_item_anchor_label(label1, opts[:list_anchor], - base[1].sub(/[^a-z0-9]*$/, ""), opts[:refer_list])] + base[0].sub(/[\p{Zs})]+$/, ""), opts[:refer_list])] else [label1, list_item_anchor_label(label1, opts[:list_anchor], opts[:prev_label], opts[:refer_list])] diff --git a/spec/isodoc/blocks_spec.rb b/spec/isodoc/blocks_spec.rb index 04fabdf..c569043 100644 --- a/spec/isodoc/blocks_spec.rb +++ b/spec/isodoc/blocks_spec.rb @@ -687,5 +687,68 @@ .new(presxml_options) .convert("test", input, true)))) .to be_equivalent_to Xml::C14n.format(presxml) + + input.sub!("", <<~SUB + + japanese + + SUB + ) + presxml = <<~INPUT + + + + japanese + + + + +
    +
  1. +
      +
    1. +
        +
      1. +
          +
        1. +
            +
          1. +
          2. +
          3. +
          4. +
          +
        2. +
        3. +
        4. +
        +
      2. +
      3. +
      4. +
      +
    2. +
    3. +
        +
      1. +
      +
    4. +
    +
  2. +
  3. +
      +
    1. +
    +
  4. +
+
+ + Contents + +
+
+ INPUT + expect(Xml::C14n.format(strip_guid(IsoDoc::Jis::PresentationXMLConvert + .new(presxml_options) + .convert("test", input, true)))) + .to be_equivalent_to Xml::C14n.format(presxml) end end