Class: Uniword::Validation::Rules::SectionPropertiesRule

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

Overview

Validates that section properties exist with required children.

DOC-104: sectPr exists with pgSz and pgMar Validity rule: R11

Constant Summary collapse

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

Instance Method Summary collapse

Methods inherited from Base

#context_type

Instance Method Details

#applicable?(context) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/uniword/validation/rules/section_properties_rule.rb', line 19

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

#categoryObject



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

def category = :structure

#check(context) ⇒ Object



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
65
66
# File 'lib/uniword/validation/rules/section_properties_rule.rb', line 23

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

  body = doc.root.at_xpath("//w:body", "w" => W_NS)
  unless body
    issues << issue("Document missing w:body element",
                    part: "word/document.xml")
    return issues
  end

  sect_pr = body.at_xpath("w:sectPr", "w" => W_NS)
  unless sect_pr
    issues << issue(
      "Document body missing w:sectPr element",
      part: "word/document.xml",
      suggestion: "Add a w:sectPr with w:pgSz and w:pgMar " \
                  "to the end of w:body.",
    )
    return issues
  end

  pg_sz = sect_pr.at_xpath("w:pgSz", "w" => W_NS)
  unless pg_sz
    issues << issue(
      "Section properties missing w:pgSz",
      part: "word/document.xml",
      suggestion: "Add <w:pgSz w:w=\"12240\" w:h=\"15840\"/>.",
    )
  end

  pg_mar = sect_pr.at_xpath("w:pgMar", "w" => W_NS)
  unless pg_mar
    issues << issue(
      "Section properties missing w:pgMar",
      part: "word/document.xml",
      suggestion: "Add <w:pgMar w:top=\"1440\" w:right=\"1440\" " \
                  "w:bottom=\"1440\" w:left=\"1440\"/>.",
    )
  end

  issues
end

#codeObject



13
# File 'lib/uniword/validation/rules/section_properties_rule.rb', line 13

def code = "DOC-104"

#descriptionObject



15
# File 'lib/uniword/validation/rules/section_properties_rule.rb', line 15

def description = "Document body must have sectPr with pgSz and pgMar"

#severityObject



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

def severity = "error"

#validity_ruleObject



14
# File 'lib/uniword/validation/rules/section_properties_rule.rb', line 14

def validity_rule = "R11"