class Wikisys::Wiki
Attributes
content[RW]
title[RW]
to_xml[R]
Public Class Methods
new(filepath='.', entries: 'entries.json', debug: false)
click to toggle source
# File lib/wikisys.rb, line 53 def initialize(filepath='.', entries: 'entries.json', debug: false) @filepath = filepath @page = '' @debug = debug @hc = HashCache.new(size:30) @entries = if entries.is_a? DxLite then entries elsif File.exists?(entries) then DxLite.new(entries) else DxLite.new('entries/entry(title, tags)') end end
Public Instance Methods
modify_build(filename)
click to toggle source
# File lib/wikisys.rb, line 118 def modify_build(filename) puts 'inside modify_buld' if @debug @title, @content, @tags = read_md(filename) # find the entry # modify the tags if necessary puts '@title: ' + @title.inspect if @debug puts '_ @content: ' + @content.inspect if @debug r = @entries.find_by_title @title puts 'r: ' + r.inspect if @debug if r.nil? then r = @entries.create title: @title, tags: @tags.join(' ') end xmlfile = filename.sub(/\.md$/,'.xml') write_xml(xmlfile, build_xml(@title, @content, @tags)) r.tags = @tags.join(' ') if r.tags != @tags end
new_build(filename)
click to toggle source
# File lib/wikisys.rb, line 143 def new_build(filename) @title, @content, @tags = read_md(filename) @entries.create title: @title, tags: @tags.join(' ') puts 'md contents: ' + [@title, @content, @tags].inspect if @debug write_xml(@title, build_xml(@title, @content, @tags)) end
page(title)
click to toggle source
# File lib/wikisys.rb, line 89 def page(title) r = @entries.find_by_title title @page = r ? read_md(title) : make_page(title) @to_xml = build_xml @page @entries.save return @page end
page=(raw_content)
click to toggle source
# File lib/wikisys.rb, line 100 def page=(raw_content) title = raw_content.lines.first.chomp r = @entries.find_by_title title make_page(title, raw_content.lines.last.chomp[/(?<=\+ )/]) unless r write_md title, raw_content title, content, tags = read_md() @to_xml = build_xml title, content, tags write_xml title, @to_xml @entries.save @page = raw_content end
read_file(file='index.html')
click to toggle source
# File lib/wikisys.rb, line 153 def read_file(file='index.html') @hc.read(file) { File.read(file) } end
to_css()
click to toggle source
# File lib/wikisys.rb, line 157 def to_css() fetch_file 'pg.css' end
write_html(filename)
click to toggle source
# File lib/wikisys.rb, line 161 def write_html(filename) FileUtils.mkdir_p File.join(@filepath, 'html') xml = read_file File.join(@filepath, 'xml', filename) puts 'about to fetch_file' if @debug xsl = fetch_file 'pg.xsl' puts 'xsl: ' + xsl.inspect if @debug html_file = File.join(@filepath, 'html', filename.sub(/\.xml$/,'.html')) write_file(html_file, transform(xsl, xml)) end
Private Instance Methods
build_xml(title, content, rawtags)
click to toggle source
# File lib/wikisys.rb, line 205 def build_xml(title, content, rawtags) puts 'content: ' + content.inspect if @debug s = content.gsub(/\[\[[^\]]+\]\]/) do |raw_link| r = @entries.find_by_title title e = Rexle::Element.new('a').add_text title e.attributes[:href] = title.gsub(/ +/, '_') if r then e.attributes[:title] = title.capitalize2 else make_page(title.capitalize2) e.attributes[:class] = 'new' e.attributes[:title] = title.capitalize2 + ' (page does not exist)' end e.xml end heading = "<heading>%s</heading>" % title if rawtags.any? then list = tags.map {|tag| " <tag>%s</tag>" % tag} tags = "<tags>\n%s\n </tags>" % list.join("\n") body = "<body>\n %s </body>" % \ Martile.new(s.lines[1..-2].join.strip).to_html else body = "<body>%s</body>" % Martile.new(s.lines[1..-1].join.strip).to_html tags = '' end "<article id='%s'>\n %s\n %s\n %s\n</article>" % \ [title.gsub(/ +/,'-'), heading, body, tags] end
make_page(title, raw_tags=title.downcase.gsub(/['\.\(\)]/,''))
click to toggle source
# File lib/wikisys.rb, line 282 def make_page(title, raw_tags=title.downcase.gsub(/['\.\(\)]/,'')) tags = raw_tags.split.join(' ') s = "#{title}\n\n\n+ " + tags write_md title, s write_xml title, build_xml(s) @entries.create title: title, tags: tags return s end
read_md(filename)
click to toggle source
# File lib/wikisys.rb, line 177 def read_md(filename) filepath = File.join(@filepath, 'md', filename) puts 'filepath : ' + filepath.inspect if @debug return unless File.exists? filepath s = read_file(filepath).strip.gsub(/\r/,'') puts 's: ' + s.inspect if @debug # read the title title = s.lines.first.chomp.sub(/^# +/,'') # read the hashtags if there is any tagsline = s.lines.last[/^ *\+ +(.*)/,1] puts 'tagsline: ' + tagsline.inspect if @debug if tagsline then [title, s.lines[1..-2].join, tagsline.split] else [title, s.lines[1..-1].join, []] end end
read_md_file(filename)
click to toggle source
# File lib/wikisys.rb, line 295 def read_md_file(filename) filepath = File.join(@filepath, 'md', filename) File.read(filepath) end
transform(xsl, xml)
click to toggle source
# File lib/wikisys.rb, line 256 def transform(xsl, xml) doc = Nokogiri::XML(xml) xslt = Nokogiri::XSLT(xsl) xslt.transform(doc) end
write_file(filepath, content)
click to toggle source
# File lib/wikisys.rb, line 275 def write_file(filepath, content) puts 'writing file: ' + filepath.inspect if @debug File.write filepath, content @hc.write(filepath) { content } end
write_md(title, content)
click to toggle source
# File lib/wikisys.rb, line 302 def write_md(title, content) puts 'inside write_md' if @debug filename = s =~ /\.md$/ ? s : s.gsub(/ +/,'_') + '.md' filepath = File.join(File.absolute_path(@filepath), 'md', filename) FileUtils.mkdir_p File.dirname(filepath) File.write filepath, content end
write_xml(s, content)
click to toggle source
# File lib/wikisys.rb, line 265 def write_xml(s, content) filename = s =~ /\.xml$/ ? s : s.gsub(/ +/,'_') + '.xml' filepath = File.join(File.absolute_path(@filepath), 'xml', filename) FileUtils.mkdir_p File.dirname(filepath) #File.write filepath, content write_file filepath, content end