Class: Uniword::Validation::Rules::BookmarksRule

Inherits:
Base
  • Object
show all
Defined in:
lib/uniword/validation/rules/bookmarks_rule.rb

Overview

Validates bookmark consistency.

DOC-040: bookmarkStart/bookmarkEnd IDs are paired DOC-041: bookmarkStart name uniqueness DOC-042: hyperlink bookmark targets exist

Constant Summary collapse

W_NS =
"http://schemas.openxmlformats.org/wordprocessingml/2006/main"

Instance Method Summary collapse

Methods inherited from Base

#description, #validity_rule

Instance Method Details

#applicable?(context) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/uniword/validation/rules/bookmarks_rule.rb', line 20

def applicable?(context)
  context.part_exists?("word/document.xml")
end

#categoryObject



17
# File 'lib/uniword/validation/rules/bookmarks_rule.rb', line 17

def category = :bookmarks

#check(context) ⇒ Object



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/uniword/validation/rules/bookmarks_rule.rb', line 24

def check(context)
  issues = []
  doc = context.document_xml
  return issues unless doc

  # DOC-040: Paired bookmarks
  start_ids = doc.root.xpath(".//w:bookmarkStart/@w:id",
                             "w" => W_NS).map(&:value)
  end_ids = doc.root.xpath(".//w:bookmarkEnd/@w:id",
                           "w" => W_NS).to_set(&:value)

  start_ids.each do |id|
    next if end_ids.include?(id)

    issues << issue(
      "bookmarkStart id='#{id}' has no matching bookmarkEnd",
      part: "word/document.xml",
      suggestion: "Add a <w:bookmarkEnd w:id='#{id}'/> to close " \
                  "this bookmark.",
    )
  end

  # DOC-041: Name uniqueness
  names = doc.root.xpath(".//w:bookmarkStart/@w:name",
                         "w" => W_NS).map(&:value)
  seen = {}
  names.each do |name|
    if seen[name]
      issues << issue(
        "Duplicate bookmark name '#{name}'",
        code: "DOC-041",
        severity: "warning",
        part: "word/document.xml",
        suggestion: "Bookmark names should be unique within a document.",
      )
    else
      seen[name] = true
    end
  end

  # DOC-042: Hyperlink anchor targets
  anchors = Set.new(names)
  doc.root.xpath(".//w:hyperlink/@w:anchor",
                 "w" => W_NS).each do |anchor|
    next if anchors.include?(anchor.value)

    issues << issue(
      "Hyperlink target bookmark '#{anchor.value}' does not exist",
      code: "DOC-042",
      part: "word/document.xml",
      suggestion: "Create a bookmark named '#{anchor.value}' or " \
                  "fix the hyperlink anchor.",
    )
  end

  issues
end

#codeObject



16
# File 'lib/uniword/validation/rules/bookmarks_rule.rb', line 16

def code = "DOC-040"

#severityObject



18
# File 'lib/uniword/validation/rules/bookmarks_rule.rb', line 18

def severity = "error"