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+|) /
- LIST_RE =
/\A#rticles#list /
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
- #paragraph_numbers_for_topics(topics, consolidate = false) ⇒ Object
- #set_up_choices ⇒ Object
- #set_up_insertions ⇒ Object
- #to_html(options = {}) ⇒ Object
- #to_yaml ⇒ Object
Instance Attribute Details
#choices ⇒ Object
Returns the value of attribute choices.
18 19 20 |
# File 'lib/rticles/document.rb', line 18 def choices @choices end |
#insertions ⇒ Object
Returns the value of attribute insertions.
18 19 20 |
# File 'lib/rticles/document.rb', line 18 def insertions @insertions end |
Class Method Details
.create_paragraphs_from_array(document, parent, array) ⇒ Object
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 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rticles/document.rb', line 79 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 list = false 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 if text_or_sub_array.match(LIST_RE) text_or_sub_array = text_or_sub_array.sub(LIST_RE, '') list = true end document.paragraphs.create( :parent_id => parent ? parent.id : nil, :body => text_or_sub_array, :name => name, :topic => topic, :heading => heading, :continuation => continuation, :list => list ) 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
70 71 72 73 74 75 76 77 |
# File 'lib/rticles/document.rb', line 70 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
21 22 23 24 |
# File 'lib/rticles/document.rb', line 21 def after_initialize set_up_insertions set_up_choices end |
#numbering_config ⇒ Object
171 172 173 |
# File 'lib/rticles/document.rb', line 171 def numbering_config @numbering_config ||= Rticles::Numbering::Config.new end |
#outline(options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rticles/document.rb', line 36 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, .merge(list: tlp.list?))) end end end o end |
#paragraph_for_reference(raw_reference) ⇒ Object
141 142 143 144 145 |
# File 'lib/rticles/document.rb', line 141 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
147 148 149 |
# File 'lib/rticles/document.rb', line 147 def paragraph_numbers_for_topic(topic, consolidate=false) paragraph_numbers_for_topics([topic], consolidate) end |
#paragraph_numbers_for_topics(topics, consolidate = false) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rticles/document.rb', line 151 def paragraph_numbers_for_topics(topics, consolidate=false) relevant_paragraphs = paragraphs.where(:topic => topics) relevant_paragraphs = relevant_paragraphs.for_choices(choices) # TODO Sorting by position won't work properly if the query includes # sub-paragraphs, but it is fine for the common case where only # top-level paragraphs have topics. # We should really be sorting by full_index, but # we don't have a sort function for this yet. (A naive typographical # sort isn't good enough.) relevant_paragraphs = relevant_paragraphs.order('position ASC') paragraph_numbers = relevant_paragraphs.map{|p| p.full_index(true, choices)}.select{|i| !i.nil?} if consolidate consolidate_paragraph_numbers(paragraph_numbers) else paragraph_numbers.join(', ') end end |
#set_up_choices ⇒ Object
31 32 33 34 |
# File 'lib/rticles/document.rb', line 31 def set_up_choices self.choices ||= {} self.choices = choices.with_indifferent_access end |
#set_up_insertions ⇒ Object
26 27 28 29 |
# File 'lib/rticles/document.rb', line 26 def set_up_insertions self.insertions ||= {} self.insertions = insertions.with_indifferent_access end |
#to_html(options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rticles/document.rb', line 53 def to_html(={}) html = "<section>" html += Rticles::Paragraph.generate_html(top_level_paragraphs, { :insertions => insertions, :choices => choices, :numbering_config => numbering_config }.merge() ) html += "</section>" html.html_safe end |
#to_yaml ⇒ Object
66 67 68 |
# File 'lib/rticles/document.rb', line 66 def to_yaml outline.to_yaml end |