Module: RRTF::ParagraphFormatting
- Included in:
- ParagraphStyle
- Defined in:
- lib/rrtf/style/formatting.rb
Overview
Encapsulates all paragraph formatting methods shared between style types.
Constant Summary
- PARAGRAPH_ATTRIBUTES =
{ "justification" => { "default" => "l", "dictionary" => { "LEFT" => "l", "RIGHT" => "r", "CENTER" => "c", "CENTRE" => "c", "FULL" => "j" }, "to_rtf" => lambda{ |value, document| "\\q#{value}" } }, "left_indent" => { "default" => nil, "to_rtf" => lambda{ |value, document| "\\li#{value}" unless value.nil? } }, "right_indent" => { "default" => nil, "to_rtf" => lambda{ |value, document| "\\ri#{value}" unless value.nil? } }, "first_line_indent" => { "default" => nil, "to_rtf" => lambda{ |value, document| "\\fi#{value}" unless value.nil? } }, "space_before" => { "default" => nil, "to_rtf" => lambda{ |value, document| "\\sb#{value}" unless value.nil? } }, "space_after" => { "default" => nil, "to_rtf" => lambda{ |value, document| "\\sa#{value}" unless value.nil? } }, "line_spacing" => { "default" => nil, "to_rtf" => lambda{ |value, document| "\\sl#{value}" unless value.nil? } }, "widow_orphan_ctl" => { "default" => nil, "to_rtf" => lambda{ |value, document| (value ? "\\widctlpar" : "\\nowidctlpar") unless value.nil? } }, "no_break" => { "default" => false, "to_rtf" => lambda{ |value, document| "\\keep" if value } }, "no_break_with_next" => { "default" => false, "to_rtf" => lambda{ |value, document| "\\keepn" if value } }, "hyphenate" => { "default" => nil, "to_rtf" => lambda{ |value, document| (value ? "\\hyphpar" : "\\hyphpar0") unless value.nil? } }, "paragraph_flow" => { "default" => 'ltr', "dictionary" => { "LEFT_TO_RIGHT" => 'ltr', "RIGHT_TO_LEFT" => 'rtl' }, "to_rtf" => lambda{ |value, document| "\\#{value}par" unless value.nil? } } }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize_paragraph_formatting(options = {}) ⇒ Object
Initializes paragraph formatting attributes.
- #paragraph_formatting_to_rtf(document) ⇒ Object
- #set_paragraph_formatting_from_hashmap(hash) ⇒ Object
Class Method Details
.included(base) ⇒ Object
259 260 261 262 263 264 265 266 |
# File 'lib/rrtf/style/formatting.rb', line 259 def self.included(base) # define accessors in base for paragraph attributes base.class_eval do PARAGRAPH_ATTRIBUTES.each do |key, | attr_accessor :#{key}" end # each end # class_eval end |
Instance Method Details
#initialize_paragraph_formatting(options = {}) ⇒ Object
Initializes paragraph formatting attributes.
283 284 285 286 287 288 289 290 |
# File 'lib/rrtf/style/formatting.rb', line 283 def initialize_paragraph_formatting( = {}) # load default attribute values PARAGRAPH_ATTRIBUTES.each do |key, | send("#{key}=", ["default"]) end # each # overwrite default attribute values with given values set_paragraph_formatting_from_hashmap() end |
#paragraph_formatting_to_rtf(document) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/rrtf/style/formatting.rb', line 307 def paragraph_formatting_to_rtf(document) text = StringIO.new # accumulate RTF representations of paragraph attributes PARAGRAPH_ATTRIBUTES.each do |key, | if .has_key?("to_rtf") rtf = ["to_rtf"].call(send(key), document) text << rtf unless rtf.nil? end # if end # each text.string end |
#set_paragraph_formatting_from_hashmap(hash) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/rrtf/style/formatting.rb', line 292 def set_paragraph_formatting_from_hashmap(hash) hash.each do |attribute, value| # skip unreconized attributes next unless(PARAGRAPH_ATTRIBUTES.keys.include?(attribute)) # preprocess value if nessesary if PARAGRAPH_ATTRIBUTES[attribute].has_key?("from_user") value = PARAGRAPH_ATTRIBUTES[attribute]["from_user"].call(value) elsif PARAGRAPH_ATTRIBUTES[attribute].has_key?("dictionary") && value.is_a?(String) value = PARAGRAPH_ATTRIBUTES[attribute]["dictionary"][value] end # if # set attribute value send("#{attribute}=", value) end # each end |