class Textigniter::Parsers::TextParser
this is the script parser
Public Instance Methods
components_parser(components, h)
click to toggle source
split out components to key/value pairs
# File lib/textigniter/parsers/text_parser.rb, line 128 def components_parser(components, h) # iterate through components components[1..-1].each do |component| # split into key value kv = component.split("\n", 2) # do some cleanup key = kv[0].strip content = parse(kv[1].strip) # store into the list h[key] = content end # return h return h end
parse(content)
click to toggle source
parse text
# File lib/textigniter/parsers/text_parser.rb, line 104 def parse(content) case $config['text_parser'] when "textile" require 'RedCloth' return RedCloth.new(content).to_html when "markdown" require 'kramdown' return Kramdown::Document.new(content).to_html when "html" return content when "txt" return content else require 'RedCloth' return RedCloth.new(content).to_html end end
plugins()
click to toggle source
# File lib/textigniter/parsers/text_parser.rb, line 4 def plugins @plugins = Textigniter::Plugins.new end
process(build_list)
click to toggle source
parse the text and build a hash to send to template rendering
# File lib/textigniter/parsers/text_parser.rb, line 9 def process(build_list) # Output message STDOUT.puts "Rendering ".yellow_on_black + "[#{$config['text_parser']}]".blue_on_black + " content ".yellow_on_black + "[OK]".green_on_black # parse for plugins plugin_parser = Textigniter::Plugins.new # create array to store processed items in items = Array.new # create an articles array for blog items blogs = Array.new # go through the build list and process build_list.each do |f| # open the file for reading file = File.open(f, 'rb') # store the contents in contents and split it at -- contents = file.read components = contents.split('--') # load the config from the text file meta = YAML::load(components[0]) # create a new hash to work with @h = Hash.new # parse through the meta meta.each do |m| @h[m[0]] = m[1] end # add config to the mix $config.each do |c| @h[c[0]] = c[1] end # created_at key @h['created_at'] = File.basename(f, ".#{$config['text_parser']}")[0..9] unless @h.has_key? 'created_at' # modified_at key @h['modified_at'] = File.mtime(f) # filename @h['manifest'] = f # extension @h['extension'] = File.extname(f) # slug key @h['slug'] = "#{File.dirname(f)}/#{File.basename(f, ".#{$config['text_parser']}")}" unless @h.has_key? 'slug' # get the template if exists else default @h['layout'] = 'default' unless @h.has_key? 'layout' # bread crumbs @h['breadcrumbs'] = @h['slug'] # gather the rest of the components into key value and convert @h = components_parser(components, @h) # run through plugin parser @h = plugin_parser.parse(@h) # set slug to directory @h['directory'] = @h['slug'] # set a handle to be used in templates @h["handle"] = @h['slug'].sub($owd, '')[1..-1] # check if blog posts according to the type key # I feel like there is probably a better way to do this. if @h.has_key? 'blog' blogs.push @h end # push processed item onto the array items.push @h end # create a super object to pass @results = Hash.new # store files to process @results['items'] = items # initialize a blog parser blog_parser = Textigniter::Parsers::BlogParser.new # store blog posts to separate key @results['blogs'] = blog_parser.parse(blogs) # return the items return @results end