Class: Textigniter::Parsers::TextParser

Inherits:
Object
  • Object
show all
Defined in:
lib/textigniter/parsers/text_parser.rb

Overview

this is the script parser

Instance Method Summary collapse

Instance Method Details

#components_parser(components, h) ⇒ Object

split out components to key/value pairs



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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) ⇒ Object

parse text



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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

#pluginsObject



4
5
6
# File 'lib/textigniter/parsers/text_parser.rb', line 4

def plugins
  @plugins = Textigniter::Plugins.new
end

#process(build_list) ⇒ Object

parse the text and build a hash to send to template rendering



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
# 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