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.



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

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

Instance Attribute Details

#clauseObject (readonly)

Returns the value of attribute clause.



235
236
237
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 235

def clause
  @clause
end

#elementObject (readonly)

Returns the value of attribute element.



235
236
237
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 235

def element
  @element
end

#style=(value) ⇒ Object (writeonly)

Sets the attribute style

Parameters:

  • value

    the value to set the attribute style to.



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

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.



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

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 ...").



301
302
303
304
305
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 301

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)


277
278
279
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 277

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

Instance Method Details

#append_text(value) ⇒ Object



250
251
252
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 250

def append_text(value)
  @text << value
end

#begin_paragraphObject



245
246
247
248
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 245

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

#commit_paragraphObject



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

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

#heading_paragraph?Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 273

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

#pending_clauseObject



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

def pending_clause
  return unless heading_paragraph?

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

#pending_elementObject



261
262
263
# File 'lib/commenter/parser/track_change_docx_parser.rb', line 261

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