Class: Slaw::Grammars::Counters

Inherits:
Object
  • Object
show all
Defined in:
lib/slaw/grammars/counters.rb

Constant Summary collapse

@@counters =

Counters for generating element IDs. This is a hash from the element ID prefix, to another hash that maps the element type name to a count.

Counters always start at 0, and must be incremented before being used. This ensures that element ids start at 1, as per AKN 3.0 spec.

eg.

section-1 => paragraph => 2
Hash.new{ |h, k| h[k] = Hash.new(0) }

Class Method Summary collapse

Class Method Details

.clean(num) ⇒ Object

Clean a <num> value for use in an eId See docs.oasis-open.org/legaldocml/akn-nc/v1.0/os/akn-nc-v1.0-os.html#_Toc531692306

β€œThe number part of the identifiers of such elements corresponds to the stripping of all final punctuation, meaningless separations as well as redundant characters in the content of the <num> element. The representation is case-sensitive.”

Our algorithm is:

  1. strip all leading and trailing whitespace and punctuation (using the unicode punctuation blocks)

  2. strip all whitespace

  3. replace all remaining punctuation with a hyphen.

The General Punctuation block is u2000-u206F, and the Supplemental Punctuation block is u2E00-u2E7F.

(i) -> i 1.2. -> 1-2 β€œ2.3β€œ -> 2-3 3a bis -> 3abis



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/slaw/grammars/counters.rb', line 43

def self.clean(num)
  # leading whitespace and punctuation
  num = num.gsub(/^[\s\u{2000}-\u{206f}\u{2e00}-\u{2e7f}!"#$%&'()*+,\-.\/:;<=>?@\[\]^_`{|}~]+/, '')
  # trailing whitespace and punctuation
  num.gsub!(/[\s\u{2000}-\u{206f}\u{2e00}-\u{2e7f}!"#$%&'()*+,\-.\/:;<=>?@\[\]^_`{|}~]+$/, '')
  # whitespace
  num.gsub!(/\s/, '')
  # remaining punctuation to a hyphen
  num.gsub!(/[\u{2000}-\u{206f}\u{2e00}-\u{2e7f}!"#$%&'()*+,\-.\/:;<=>?@\[\]^_`{|}~]+/, '-')
  num
end

.countersObject



16
17
18
# File 'lib/slaw/grammars/counters.rb', line 16

def self.counters
  @@counters
end

.reset!Object



20
21
22
# File 'lib/slaw/grammars/counters.rb', line 20

def self.reset!
  @@counters.clear
end