Class: Commenter::Parser

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

Instance Method Summary collapse

Instance Method Details

#parse(docx_path, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/commenter/parser.rb', line 9

def parse(docx_path, options = {})
  doc = Docx::Document.open(docx_path)

  # Extract metadata from the first table
   = (doc)

  # The comments are in the second table (or first table if there's only one)
  comments_table = doc.tables.length > 1 ? doc.tables[1] : doc.tables.first
  raise "No comments table found in document" unless comments_table
  raise "Comments table appears to be empty" if comments_table.row_count < 2

  comments = []

  # Process all rows - don't skip any rows, respect all content
  (0..comments_table.row_count - 1).each do |i|
    row = comments_table.rows[i]
    cells = row.cells.map { |c| c.text.strip }

    # Skip only completely empty rows
    next if cells.all?(&:empty?)

    # Extract body from ID (e.g., "DE-001" -> "DE")
    id = cells[0] || ""
    body = id.include?("-") ? id.split("-").first : id

    # Create comment with symbol keys, respecting all input data
    comment_attrs = {
      id: id,
      body: body,
      locality: {
        line_number: cells[1] && cells[1].empty? ? nil : cells[1],
        clause: cells[2] && cells[2].empty? ? nil : cells[2],
        element: cells[3] && cells[3].empty? ? nil : cells[3]
      },
      type: cells[4] || "",
      comments: cells[5] || "",
      proposed_change: cells[6] || ""
    }

    # Handle observations column
    unless options[:exclude_observations]
      comment_attrs[:observations] = cells[7] && cells[7].empty? ? nil : cells[7]
    end

    comments << Comment.new(comment_attrs)
  end

  # Create comment sheet
  CommentSheet.new(
    version: "2012-03",
    date: [:date],
    document: [:document],
    project: [:project],
    comments: comments
  )
end