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
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
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
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
|