Class: MyOutline

Inherits:
Object
  • Object
show all
Defined in:
lib/myoutline.rb

Defined Under Namespace

Classes: Outline

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, debug: false, allsorted: true, autoupdate: true, topic_url: '$topic', md_path: '.', default_md: 'main.md') ⇒ MyOutline



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/myoutline.rb', line 120

def initialize(source, debug: false, allsorted: true, autoupdate: true, 
               topic_url: '$topic', md_path: '.', default_md: 'main.md')
  
  @debug, @topic_url = debug, topic_url
  @md_path = md_path
  @default_md = default_md
  @allsorted, @autoupdate = allsorted, autoupdate

  @outline = Outline.new(source, debug: debug, 
                         allsorted: allsorted, autoupdate: autoupdate)

end

Instance Attribute Details

#mdObject

Returns the value of attribute md.



15
16
17
# File 'lib/myoutline.rb', line 15

def md
  @md
end

#outlineObject (readonly)

Returns the value of attribute outline.



14
15
16
# File 'lib/myoutline.rb', line 14

def outline
  @outline
end

Instance Method Details

#fetch(uri) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/myoutline.rb', line 133

def fetch(uri)

  s, remaining = @outline.locate(uri)
  puts 'fetch() s: ' + s.inspect if @debug
  redirect = s =~ /^\[r\] +/i
  return s if redirect 
  
  s ||= @default_md; remaining ||= ''
  
  f = File.join(@md_path, s)
  puts 'f: ' + f.inspect if @debug
  @md = MdEdit.new f, debug: @debug
  r = edit(remaining.sub(/^\//,'').gsub(/\//,' > '))    
  puts 'fetch() r: ' + r.inspect if @debug
  @md.update r
  
  r
end

#rebuild(s) ⇒ Object



152
153
154
# File 'lib/myoutline.rb', line 152

def rebuild(s)
  @outline = Outline.new s
end

#to_htmlObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/myoutline.rb', line 185

def to_html()

  px = self.outline.to_px
  
  px.each_recursive do |x, parent|
    
    if x.is_a? Entry then
      
      trail = parent.attributes[:trail]
      s = x.title.gsub(/ +/,'-')
      x.attributes[:trail] = trail.nil? ? s : trail + '/' + s
      
    end
    
  end

  doc  = Nokogiri::XML(px.to_xml)
  xsl  = Nokogiri::XSLT(xslt())

  html_doc = Rexle.new(xsl.transform(doc).to_s)
      
  html_doc.root.css('.atopic').each do |e|      
    
    puts 'e: ' + e.parent.parent.xml.inspect if @debug
    
    href = e.attributes[:href]
    if href.empty? or href[0] == '!' then
      
      e.attributes[:href] = @topic_url.sub(/\$topic/, e.text)\
          .sub(/\$id/, e.attributes[:id]).sub(/\$trail/, e.attributes[:trail])\
          .to_s.gsub(/ +/,'-')
      
    end
  end
  
  html_doc.xml(pretty: true, declaration: false)
  
end

#update(section) ⇒ Object



156
157
158
# File 'lib/myoutline.rb', line 156

def update(section)
  @md.update section
end

#update_tree(s) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/myoutline.rb', line 160

def update_tree(s)
  
  mo2 = Outline.new(s, debug: @debug, 
                         allsorted: @allsorted, autoupdate: @autoupdate)
  
  h = @outline.links.to_h
  links = mo2.links
  
  mo2.links.to_h.each do |title, file|
    
    if @debug then
      puts 'title: ' + title.inspect
      puts 'h[title]: ' + h[title].inspect
    end
    
   links.link(title).url = h[title] if h[title]
  end
  
  puts 'before Outline.new: ' + links.to_s(header: false).inspect if @debug
  
  @outline  = Outline.new(links.to_s(header: false), debug: @debug, 
                         allsorted: @allsorted, autoupdate: @autoupdate)
  @outline.autosave
end