Class: Commenter::Parser::TrackChangeDocxParser::ParagraphState

Inherits:
Object
  • Object
show all
Defined in:
lib/commenter/parser/track_change_docx_parser.rb

Overview

Tracks the current paragraph's style and text to resolve the locality context of a change:

  • clause: the clause number of the most recent heading paragraph; an unnumbered sub-heading inherits its parent heading's clause
  • element: the nearest Table/Figure/Formula/NOTE reference, scoped to the current clause (caption paragraphs and inline mentions both count)

Constant Summary collapse

NUMBERED_CLAUSE =
/\A(?:\d+(?:\.\d+)*|Annex\s+[A-Z](?:\.\d+)*|Bibliography|Foreword|Introduction)\z/i
ELEMENT_START =
/\A\s*(NOTE\s+\d+|NOTE(?=\s*[—:-])|Table\s+(?:[A-Z]\.)?\d+(?:\.\d+)*|Figure\s+(?:[A-Z]\.)?\d+(?:\.\d+)*)\b/i
FORMULA =
/\b(Formula\s*\(\d+(?:\.\d+)*\))/
ELEMENT_ANY =
/\b(Table\s+(?:[A-Z]\.)?\d+(?:\.\d+)*|Figure\s+(?:[A-Z]\.)?\d+(?:\.\d+)*|NOTE\s+\d+)\b/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParagraphState

Returns a new instance of ParagraphState.



235
236
237
238
239
240
241
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 235

def initialize
  @style = nil
  @text = +""
  @clause = ""
  @element = nil
  @heading_stack = []
end

Instance Attribute Details

#clauseObject (readonly)

Returns the value of attribute clause.



233
234
235
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 233

def clause
  @clause
end

#elementObject (readonly)

Returns the value of attribute element.



233
234
235
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 233

def element
  @element
end

#style=(value) ⇒ Object (writeonly)

Sets the attribute style

Parameters:

  • value

    the value to set the attribute style to.



232
233
234
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 232

def style=(value)
  @style = value
end

Class Method Details

.clause_for(text) ⇒ Object

Extracts the clause identifier from heading text: the leading number ("4.2.1 Thermodynamic ..." -> "4.2.1"), an annex reference, or a well-known unnumbered section. Runs may be concatenated without spaces ("4.2.1Thermodynamic"), so the number is matched with a lookahead.



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 284

def self.clause_for(text)
  s = text.to_s.strip
  return "" if s.empty?

  return Regexp.last_match(1) if s =~ /\A\s*(Annex\s+[A-Z](?:\.\d+)*)\b/i
  return Regexp.last_match(1) if s =~ /\A\s*(Bibliography|Foreword|Introduction)\b/i
  return Regexp.last_match(1) if s =~ /\A\s*((?:\d+\.)*\d+)(?=[A-Z\s])/
  return Regexp.last_match(1) if s =~ /\A\s*((?:\d+\.)*\d+)\b/

  s
end

.element_for(text) ⇒ Object

Resolves the element reference of a paragraph: a caption opening the paragraph ("Table 3 — ...", "NOTE 2 ...", "Formula (9):") wins over an inline mention ("the values in Table 5 shall ...").



299
300
301
302
303
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 299

def self.element_for(text)
  s = text.to_s
  match = s.match(ELEMENT_START) || s.match(FORMULA) || s.match(ELEMENT_ANY)
  match[1].squeeze(" ").strip if match
end

.numbered_clause?(clause) ⇒ Boolean

Returns:

  • (Boolean)


275
276
277
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 275

def self.numbered_clause?(clause)
  clause.to_s.match?(NUMBERED_CLAUSE)
end

Instance Method Details

#append_text(value) ⇒ Object



248
249
250
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 248

def append_text(value)
  @text << value
end

#begin_paragraphObject



243
244
245
246
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 243

def begin_paragraph
  @style = nil
  @text = +""
end

#commit_paragraphObject



263
264
265
266
267
268
269
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 263

def commit_paragraph
  if heading_paragraph?
    commit_heading
  else
    @element = self.class.element_for(@text) || @element
  end
end

#heading_paragraph?Boolean

Returns:

  • (Boolean)


271
272
273
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 271

def heading_paragraph?
  @style&.match?(HEADING_STYLE)
end

#pending_clauseObject



252
253
254
255
256
257
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 252

def pending_clause
  return unless heading_paragraph?

  clause = self.class.clause_for(@text)
  clause unless clause.empty?
end

#pending_elementObject



259
260
261
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 259

def pending_element
  self.class.element_for(@text)
end