Module: Duxml

Includes:
Meta, Saxer
Included in:
ElementGuts, Grammar, History, Pattern, PatternMaker, RelaxNG
Defined in:
lib/duxml.rb,
lib/duxml/doc.rb,
lib/duxml/meta.rb,
lib/duxml/saxer.rb,
lib/duxml/doc/element.rb,
lib/duxml/doc/lazy_ox.rb,
lib/duxml/doc/node_set.rb,
lib/duxml/meta/grammar.rb,
lib/duxml/meta/history.rb,
lib/duxml/meta/history/add.rb,
lib/duxml/meta/grammar/rule.rb,
lib/duxml/meta/history/undo.rb,
lib/duxml/meta/history/error.rb,
lib/duxml/meta/history/change.rb,
lib/duxml/meta/history/remove.rb,
lib/duxml/meta/grammar/pattern.rb,
lib/duxml/meta/grammar/relax_ng.rb,
lib/duxml/meta/history/new_attr.rb,
lib/duxml/meta/history/new_text.rb,
lib/duxml/meta/grammar/spreadsheet.rb,
lib/duxml/meta/history/change_attr.rb,
lib/duxml/meta/history/change_text.rb,
lib/duxml/meta/grammar/pattern_maker.rb,
lib/duxml/meta/history/qualify_error.rb,
lib/duxml/meta/grammar/rule/text_rule.rb,
lib/duxml/meta/history/validate_error.rb,
lib/duxml/meta/grammar/rule/attrs_rule.rb,
lib/duxml/meta/grammar/rule/value_rule.rb,
lib/duxml/meta/grammar/rule/children_rule.rb,
lib/duxml/meta/grammar/relax_ng/attrs_rule.rb,
lib/duxml/meta/grammar/relax_ng/value_rule.rb,
lib/duxml/meta/grammar/pattern/text_pattern.rb,
lib/duxml/meta/grammar/pattern/child_pattern.rb,
lib/duxml/meta/grammar/relax_ng/children_rule.rb,
lib/duxml/meta/grammar/pattern/attr_val_pattern.rb,
lib/duxml/meta/grammar/pattern/attr_name_pattern.rb

Defined Under Namespace

Modules: Add, AttrNamePattern, AttrValPattern, AttrsRule, Change, ChangeAttr, ChangeText, ChildPattern, ChildrenRule, ElementGuts, Error, Grammar, History, LazyOx, Meta, NewAttr, NewText, Pattern, PatternMaker, QualifyError, RelaxNG, Remove, RngAttrsRule, RngChildrenRule, RngValueRule, Rule, Saxer, Spreadsheet, TextPattern, TextRule, Undo, ValidateError, ValueRule Classes: AddClass, AttrNamePatternClass, AttrValPatternClass, AttrsRuleClass, ChangeAttrClass, ChangeClass, ChangeTextClass, ChildPatternClass, ChildrenRuleClass, Doc, Element, ErrorClass, GrammarClass, HistoryClass, MetaClass, NewAttrClass, NewTextClass, NodeSet, NullChildPatternClass, PatternClass, QualifyErrorClass, RemoveClass, RuleClass, TextPatternClass, TextRuleClass, UndoClass, ValidateErrorClass, ValueRuleClass

Constant Summary collapse

DITA_GRAMMAR =
File.expand_path(File.dirname(__FILE__) + '/../xml/dita_grammar.xml')

Constants included from Meta

Meta::FILE_EXT

Instance Attribute Summary collapse

Attributes included from Saxer

#io

Instance Method Summary collapse

Methods included from Meta

#grammar=, meta_path, xml

Methods included from Saxer

#sax

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



18
19
20
# File 'lib/duxml.rb', line 18

def doc
  @doc
end

#fileObject (readonly)

Returns the value of attribute file.



18
19
20
# File 'lib/duxml.rb', line 18

def file
  @file
end

#metaObject (readonly)

Returns the value of attribute meta.



18
19
20
# File 'lib/duxml.rb', line 18

def meta
  @meta
end

Instance Method Details

#create(file, content = nil) ⇒ Object

Parameters:

  • file (String)

    creates new XML file at given path

  • content (Doc, Element) (defaults to: nil)

    XML content with which to initialize new file



37
38
39
40
# File 'lib/duxml.rb', line 37

def create(file, content=nil)
  File.write(file, content.to_s)
  @doc = content.is_a?(Doc) ? content : Doc.new
end

#load(_file, grammar_path = nil) ⇒ Duxml::Meta

Returns combined Object tree from metadata root (metadata and content’s XML documents are kept separate).

Parameters:

  • file (String, Doc)

    loads or creates given file or document and finds or creates corresponding metadata file e.g. ‘.xml_file.duxml’

  • grammar_path (nil, String, Duxml::Grammar) (defaults to: nil)

    optional - provide an external grammar file or object

Returns:

  • (Duxml::Meta)

    combined Object tree from metadata root (metadata and content’s XML documents are kept separate)



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/duxml.rb', line 23

def load(_file, grammar_path=nil)
  if _file.is_a?(String) and File.exists?(_file)
    @file = _file
  else
    @file = "#{(_file.respond_to?(:name) ? _file.name : _file.class.to_s) + _file.object_id.to_s}"
    File.write file, ''
  end

  set_metadata!(grammar_path)
  set_doc!
end

#log(file) ⇒ Object

Parameters:

  • file (String)

    output file path for logging human-readable validation error messages



52
53
54
# File 'lib/duxml.rb', line 52

def log(file)
  File.write(file, meta.history.description)
end

#relaxngNokogiri::XML::RelaxNG

Returns current metadata’s grammar as a relaxng file.

Returns:

  • (Nokogiri::XML::RelaxNG)

    current metadata’s grammar as a relaxng file



68
69
70
# File 'lib/duxml.rb', line 68

def relaxng
  #meta.grammar.relaxng

end

#save(file) ⇒ Object

Parameters:

  • file (String)

    saves current content XML to given file path (Duxml@file by default)



43
44
45
46
47
48
49
# File 'lib/duxml.rb', line 43

def save(file)
  meta_path = Meta.meta_path(file)
  unless File.exists?(meta_path)
    File.new meta_path, 'w+'
    File.write(meta_path, Meta.xml)
  end
end

#validate(*args) ⇒ Boolean

Returns whether file passed validation.

Parameters:

  • *Args (*several_variants)

    @see #load

Returns:

  • (Boolean)

    whether file passed validation

Raises:

  • (Exception)


58
59
60
61
62
63
64
65
# File 'lib/duxml.rb', line 58

def validate(*args)
  load(*args) unless args.empty?
  raise Exception, "grammar not defined!" unless meta.grammar.defined?
  raise Exception, "document not loaded!" unless doc.root
  results = []
  doc.root.traverse do |n| results << meta.grammar.validate(n) unless n.is_a?(String) end
  !results.any? do |r| !r end
end