Class: Rticles::Document
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Rticles::Document
- Defined in:
- lib/rticles/document.rb
Constant Summary collapse
- NAME_RE =
/\A#rticles#name#([A-Za-z_]+) /
- TOPIC_RE =
/\A#rticles#topic#([A-Za-z_]+) /
- CONTINUATION_RE =
/\A#rticles#continue /
- HEADING_RE =
/\A#rticles#heading(#\d+|) /
Instance Attribute Summary collapse
-
#choices ⇒ Object
Returns the value of attribute choices.
-
#insertions ⇒ Object
Returns the value of attribute insertions.
Class Method Summary collapse
Instance Method Summary collapse
- #after_initialize ⇒ Object
- #numbering_config ⇒ Object
- #outline(options = {}) ⇒ Object
- #paragraph_for_reference(raw_reference) ⇒ Object
- #paragraph_numbers_for_topic(topic, consolidate = false) ⇒ Object
- #set_up_choices ⇒ Object
- #set_up_insertions ⇒ Object
- #to_html ⇒ Object
- #to_yaml ⇒ Object
Instance Attribute Details
#choices ⇒ Object
Returns the value of attribute choices.
17 18 19 |
# File 'lib/rticles/document.rb', line 17 def choices @choices end |
#insertions ⇒ Object
Returns the value of attribute insertions.
17 18 19 |
# File 'lib/rticles/document.rb', line 17 def insertions @insertions end |
Class Method Details
.create_paragraphs_from_array(document, parent, array) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 123 124 125 126 127 128 |
# File 'lib/rticles/document.rb', line 76 def self.create_paragraphs_from_array(document, parent, array) array.each do |text_or_sub_array| case text_or_sub_array when String name = nil topic = nil continuation = false heading = nil if name_match = text_or_sub_array.match(NAME_RE) text_or_sub_array = text_or_sub_array.sub(NAME_RE, '') name = name_match[1] end if topic_match = text_or_sub_array.match(TOPIC_RE) text_or_sub_array = text_or_sub_array.sub(TOPIC_RE, '') topic = topic_match[1] end if text_or_sub_array.match(CONTINUATION_RE) text_or_sub_array = text_or_sub_array.sub(CONTINUATION_RE, '') continuation = true end if heading_match = text_or_sub_array.match(HEADING_RE) text_or_sub_array = text_or_sub_array.sub(HEADING_RE, '') if heading_match[1].empty? heading = 1 else heading = heading_match[1].sub(/\A#/, '').to_i end end document.paragraphs.create( :parent_id => parent ? parent.id : nil, :body => text_or_sub_array, :name => name, :topic => topic, :heading => heading, :continuation => continuation ) when Array paragraphs_relation = parent ? parent.children : document.paragraphs.select{|p| p.parent_id.nil?} if paragraphs_relation.empty? raise RuntimeError, "jump in nesting at: #{text_or_sub_array.first}" end create_paragraphs_from_array( document, paragraphs_relation.last, text_or_sub_array ) end end end |
.from_yaml(yaml) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/rticles/document.rb', line 67 def self.from_yaml(yaml) parsed_yaml = YAML.load(yaml) document = self.create create_paragraphs_from_array(document, nil, parsed_yaml) document end |
Instance Method Details
#after_initialize ⇒ Object
20 21 22 23 |
# File 'lib/rticles/document.rb', line 20 def after_initialize set_up_insertions set_up_choices end |
#numbering_config ⇒ Object
148 149 150 |
# File 'lib/rticles/document.rb', line 148 def numbering_config @numbering_config ||= Rticles::Numbering::Config.new end |
#outline(options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rticles/document.rb', line 35 def outline(={}) = .with_indifferent_access for_display = [:for_display] o = [] top_level_paragraphs.each do |tlp| body = for_display ? tlp.body_for_display({:insertions => insertions, :choices => choices}.merge()) : tlp.body if body o.push(for_display ? tlp.body_for_display({:insertions => insertions, :choices => choices}.merge()) : tlp.body) unless tlp.children.empty? o.push(sub_outline(tlp, )) end end end o end |
#paragraph_for_reference(raw_reference) ⇒ Object
130 131 132 133 134 |
# File 'lib/rticles/document.rb', line 130 def paragraph_for_reference(raw_reference) # TODO optimise Rails.logger.debug("Finding raw reference: #{raw_reference}") paragraphs.all.detect{|p| p.full_index == raw_reference} end |
#paragraph_numbers_for_topic(topic, consolidate = false) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rticles/document.rb', line 136 def paragraph_numbers_for_topic(topic, consolidate=false) relevant_paragraphs = paragraphs.where(:topic => topic) relevant_paragraphs = relevant_paragraphs.for_choices(choices) paragraph_numbers = relevant_paragraphs.map{|p| p.full_index(true, choices)}.select{|i| !i.nil?}.sort if consolidate consolidate_paragraph_numbers(paragraph_numbers) else paragraph_numbers.join(', ') end end |
#set_up_choices ⇒ Object
30 31 32 33 |
# File 'lib/rticles/document.rb', line 30 def set_up_choices self.choices ||= {} self.choices = choices.with_indifferent_access end |
#set_up_insertions ⇒ Object
25 26 27 28 |
# File 'lib/rticles/document.rb', line 25 def set_up_insertions self.insertions ||= {} self.insertions = insertions.with_indifferent_access end |
#to_html ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rticles/document.rb', line 52 def to_html html = "<section>" html += Rticles::Paragraph.generate_html(top_level_paragraphs, :insertions => insertions, :choices => choices, :numbering_config => numbering_config ) html += "</section>" html.html_safe end |
#to_yaml ⇒ Object
63 64 65 |
# File 'lib/rticles/document.rb', line 63 def to_yaml outline.to_yaml end |