Class: RRTF::Stylesheet
- Inherits:
-
Object
- Object
- RRTF::Stylesheet
- Defined in:
- lib/rrtf/stylesheet.rb
Overview
Represents a stylesheet in an RTF document.
Instance Attribute Summary collapse
-
#document ⇒ Document
The document to which the stylesheet belongs.
-
#styles ⇒ Hash<String, Style>
readonly
Stores the Style objects associated with the stylesheet, each of which is keyed by its assigned ID.
Instance Method Summary collapse
-
#add_style(options) ⇒ Object
Adds a single style to the stylesheet.
-
#add_styles(hash_array) ⇒ Object
Adds the specified styles to the stylesheet.
-
#initialize(document, options = {}) ⇒ Stylesheet
constructor
Builds a Stylesheet object.
-
#to_rtf(options = {}) ⇒ Object
Converts the stylesheet to its RTF representation.
Constructor Details
#initialize(document, options = {}) ⇒ Stylesheet
Builds a Stylesheet object.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rrtf/stylesheet.rb', line 27 def initialize(document, = {}) @options = { "styles" => [], "base_style_handle" => 1, "base_style_priority" => 1, "assign_style_handles" => true, "assign_style_priorities" => true }.merge() @document = document @next_styles_hash = {} @base_styles_hash = {} @styles = {} add_styles(@options["styles"]) end |
Instance Attribute Details
#document ⇒ Document
The document to which the stylesheet belongs.
15 16 17 |
# File 'lib/rrtf/stylesheet.rb', line 15 def document @document end |
#styles ⇒ Hash<String, Style> (readonly)
Stores the Style objects associated with the stylesheet, each of which is keyed by its assigned ID.
11 12 13 |
# File 'lib/rrtf/stylesheet.rb', line 11 def styles @styles end |
Instance Method Details
#add_style(options) ⇒ Object
Adds a single style to the stylesheet.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rrtf/stylesheet.rb', line 59 def add_style() style = .delete("style") type = .delete("type") = () if !style.nil? # style object given; add directly if !add_style_object(style, ) RTFError.fire("#{style.to_s} could not be added to the stylesheet (hint: make sure it's a style object).") end # if elsif !type.nil? # style object not given; create based on type case type when "paragraph" add_style_object(ParagraphStyle.new(), ) when "character" add_style_object(CharacterStyle.new(), ) else RTFError.fire("Unreconized style type '#{type.to_s}'.") end # case else RTFError.fire("A style type or style object must be specified for each style in a stylesheet.") end # if end |
#add_styles(hash_array) ⇒ Object
Adds the specified styles to the stylesheet.
46 47 48 |
# File 'lib/rrtf/stylesheet.rb', line 46 def add_styles(hash_array) hash_array.each { |hash| add_style(hash) } end |
#to_rtf(options = {}) ⇒ Object
calling ‘to_rtf` causes all next and base styles to be updated (to_rtf “commits” the stylesheet); errors might be raised if next or base styles are missing (have yet to be added to the stylesheet).
Converts the stylesheet to its RTF representation.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rrtf/stylesheet.rb', line 93 def to_rtf( = {}) # load default options = { "uglify" => false, "base_indent" => 0, "child_indent" => 0 }.merge() # build line prefixes newline_prefix = ["uglify"] ? '' : "\n" base_prefix = ["uglify"] ? '' : " "*["base_indent"] # lookup and set next and base style handles on component styles substitute_next_style_handles() substitute_base_style_handles() rtf = StringIO.new rtf << "#{base_prefix}{\\stylesheet" @styles.values.each do |style| rtf << newline_prefix rtf << style.to_rtf( document, "uglify" => ["uglify"], "base_indent" => ["base_indent"]+["child_indent"] ) end rtf << "#{newline_prefix}#{base_prefix}}" rtf.string end |