Skip to content

Commit

Permalink
list numbering in Japanese numbers: #228
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Oct 24, 2024
1 parent 18312e9 commit 27a47c2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
42 changes: 30 additions & 12 deletions lib/isodoc/jis/xref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,41 @@ 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

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 ? "&#x30fb;" : "."
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
Expand Down Expand Up @@ -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)
Expand All @@ -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])]
Expand Down
63 changes: 63 additions & 0 deletions spec/isodoc/blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -687,5 +687,68 @@
.new(presxml_options)
.convert("test", input, true))))
.to be_equivalent_to Xml::C14n.format(presxml)

input.sub!("<preface>", <<~SUB
<metanorma-extension>
<presentation-metadata><autonumbering-style>japanese</autonumbering-style></presentation-metadata>
</metanorma-extension><preface>
SUB
)
presxml = <<~INPUT
<iso-standard xmlns="http://riboseinc.com/isoxml" type="presentation">
<metanorma-extension>
<presentation-metadata>
<autonumbering-style>japanese</autonumbering-style>
</presentation-metadata>
</metanorma-extension>
<preface>
<foreword displayorder="1">
<ol id="A" type="alphabet">
<li id="A1" label="a">
<ol id="B" type="arabic">
<li id="B1" label="一">
<ol id="C" type="arabic">
<li id="C1" label="一・一">
<ol id="D" type="arabic">
<li id="D1" label="一・一・一">
<ol id="E" type="arabic">
<li id="E1" label="一・一・一・一">
</li>
<li id="E2" label="一・一・一・二">
</li>
</ol>
</li>
<li id="D2" label="一・一・二">
</li>
</ol>
</li>
<li id="C2" label="一・二">
</li>
</ol>
</li>
<li id="B2" label="二">
<ol id="CC" type="arabic">
<li id="CC1" label="二・一"/>
</ol>
</li>
</ol>
</li>
<li id="A2" label="b">
<ol id="BB" type="arabic">
<li id="BB1" label="一"/>
</ol>
</li>
</ol>
</foreword>
<clause type="toc" id="_" displayorder="2">
<title depth="1">Contents</title>
</clause>
</preface>
</iso-standard>
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

0 comments on commit 27a47c2

Please sign in to comment.