Class: Newstile::Document
- Inherits:
-
Object
- Object
- Newstile::Document
- Defined in:
- lib/newstile/document.rb
Overview
The main interface to newstile.
This class provides a one-stop-shop for using newstile to convert text into various output formats. Use it like this:
require 'newstile'
doc = Newstile::Document.new('This *is* some newstile text')
puts doc.to_html
The #to_html method is a shortcut for using the Converter::Html class.
The second argument to the #new method is an options hash for customizing the behaviour of the used parser and the converter. See Document#new for more information!
Instance Attribute Summary collapse
-
#conversion_infos ⇒ Object
readonly
Holds conversion information which is dependent on the used converter.
-
#options ⇒ Object
readonly
The options hash which holds the options for parsing/converting the Newstile document.
-
#parse_infos ⇒ Object
readonly
Holds needed parse information which is dependent on the used parser, like ALDs, link definitions and so on.
-
#tree ⇒ Object
The element tree of the document.
-
#warnings ⇒ Object
readonly
An array of warning messages.
Instance Method Summary collapse
-
#initialize(source, options = {}) ⇒ Document
constructor
Create a new Newstile document from the string
source
and use the providedoptions
. -
#inspect ⇒ Object
:nodoc:.
-
#method_missing(id, *attr, &block) ⇒ Object
Check if a method is invoked that begins with
to_
and if so, try to instantiate a converter class (i.e. a class in the Newstile::Converter module) and use it for converting the document.
Constructor Details
#initialize(source, options = {}) ⇒ Document
Create a new Newstile document from the string source
and use the provided options
. The options that can be used are defined in the Options module.
The special options key :input
can be used to select the parser that should parse the source
. It has to be the name of a class in the Newstile::Parser module. For example, to select the newstile parser, one would set the :input
key to Newstile
. If this key is not set, it defaults to Newstile
.
The source
is immediately parsed by the selected parser so that the document tree is immediately available and the output can be generated.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/newstile/document.rb', line 92 def initialize(source, = {}) @options = Options.merge() @warnings = [] @parse_infos = {} @parse_infos[:encoding] = source.encoding if RUBY_VERSION >= '1.9' @conversion_infos = {} parser = ([:input] || 'newstile').to_s parser = parser[0..0].upcase + parser[1..-1] if Parser.const_defined?(parser) @tree = Parser.const_get(parser).parse(source, self) else raise Newstile::Error.new("newstile has no parser to handle the specified input format: #{[:input]}") end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *attr, &block) ⇒ Object
Check if a method is invoked that begins with to_
and if so, try to instantiate a converter class (i.e. a class in the Newstile::Converter module) and use it for converting the document.
For example, to_html
would instantiate the Newstile::Converter::Html class.
111 112 113 114 115 116 117 |
# File 'lib/newstile/document.rb', line 111 def method_missing(id, *attr, &block) if id.to_s =~ /^to_(\w+)$/ Converter.const_get($1[0..0].upcase + $1[1..-1]).convert(self) else super end end |
Instance Attribute Details
#conversion_infos ⇒ Object (readonly)
Holds conversion information which is dependent on the used converter. A converter clears this variable before doing the conversion.
79 80 81 |
# File 'lib/newstile/document.rb', line 79 def conversion_infos @conversion_infos end |
#options ⇒ Object (readonly)
The options hash which holds the options for parsing/converting the Newstile document. It is possible that these values get changed during the parsing phase.
67 68 69 |
# File 'lib/newstile/document.rb', line 67 def @options end |
#parse_infos ⇒ Object (readonly)
Holds needed parse information which is dependent on the used parser, like ALDs, link definitions and so on. This information may be used by converters afterwards.
75 76 77 |
# File 'lib/newstile/document.rb', line 75 def parse_infos @parse_infos end |
#tree ⇒ Object
The element tree of the document. It is immediately available after the #new method has been called.
63 64 65 |
# File 'lib/newstile/document.rb', line 63 def tree @tree end |
#warnings ⇒ Object (readonly)
An array of warning messages. It is filled with warnings during the parsing phase (i.e. in #new) and the conversion phase.
71 72 73 |
# File 'lib/newstile/document.rb', line 71 def warnings @warnings end |
Instance Method Details
#inspect ⇒ Object
:nodoc:
119 120 121 |
# File 'lib/newstile/document.rb', line 119 def inspect #:nodoc: "<KD:Document: options=#{@options.inspect} tree=#{@tree.inspect} warnings=#{@warnings.inspect}>" end |