Module: Kitabu::Base

Defined in:
lib/kitabu/base.rb

Constant Summary collapse

DEFAULT_LAYOUT =
'boom'
DEFAULT_THEME =
'eiffel'
DEFAULT_SYNTAX =
'plain_text'
GEM_ROOT =
File.expand_path(File.dirname(__FILE__) + "/../../")

Class Method Summary collapse

Class Method Details

.app_nameObject



227
228
229
# File 'lib/kitabu/base.rb', line 227

def self.app_name
  ENV['KITABU_NAME'] || 'kitabu'
end

.configObject



85
86
87
# File 'lib/kitabu/base.rb', line 85

def self.config
  @config ||= YAML::load_file(config_path)
end

.config_pathObject



77
78
79
# File 'lib/kitabu/base.rb', line 77

def self.config_path
  KITABU_ROOT + "/config.yml"
end

.default_layoutObject



247
248
249
# File 'lib/kitabu/base.rb', line 247

def self.default_layout
  DEFAULT_LAYOUT
end

.default_syntaxObject



243
244
245
# File 'lib/kitabu/base.rb', line 243

def self.default_syntax
  DEFAULT_SYNTAX
end

.default_themeObject



239
240
241
# File 'lib/kitabu/base.rb', line 239

def self.default_theme
  DEFAULT_THEME
end

.generate_htmlObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
223
224
225
# File 'lib/kitabu/base.rb', line 131

def self.generate_html
  # all parsed markdown file holder
  contents = ""
  
  # first, get all chapters; then, get all parsed markdown
  # files from this chapter and group them into a <div class="chapter"> tag
  Dir.entries(text_dir).sort.each do |dirname|
    # ignore files and some directories
    next if %w(. .. .svn .git).include?(dirname) || File.file?(text_dir + "/#{dirname}")
    
    # gets all parsed markdown files to wrap in a 
    # chapter element
    chapter = ""
    
    # merge all markdown and textile files into a single list
    markup_files = Dir["#{text_dir}/#{dirname}/**/*.markdown"] + Dir["#{text_dir}/#{dirname}/**/*.textile"]
    
    # no files, so skip it!
    next if markup_files.empty?
    
    markup_files.sort.each do |markup_file|
      # get the file contents
      markup_contents = File.new(markup_file).read
      
      # instantiate a markup object
      begin
        if markup_file =~ /\.textile$/
          markup = BlackCloth.new(markup_contents)
        else
          markup = Discount.new(markup_contents)
        end
      rescue Exception => e
        puts "Skipping #{markup_file} (#{e.message})"
        next
      end
      
      # convert the markup into html
      parsed_contents = markup.to_html
      
      if Object.const_defined?('Uv')
        if markup.respond_to?(:syntax_blocks)
          # textile
          parsed_contents.gsub!(/@syntax:([0-9]+)/m) do |m|
            syntax, code = markup.syntax_blocks[$1.to_i]
            Kitabu::Markup.syntax(code, syntax)
          end
        else
          # markdown
          parsed_contents.gsub! /<pre><code>(.*?)<\/code><\/pre>/m do |block|
            code = $1.gsub(/&lt;/, '<').gsub(/&gt;/, '>').gsub(/&amp;/, '&')
            code_lines = StringIO.new(code).readlines
            syntax_settings = code_lines.first
            
            syntax = 'plain_text'
            
            if syntax_settings =~ /syntax\(.*?\)\./
              code = code_lines.slice(1, code_lines.size).join
              
              # syntax
              m, syntax = *syntax_settings.match(/syntax\(([^ #]+).*?\)./)
              
              # file name
              m, source_file = *syntax_settings.match(/syntax\(.*?\)\. +(.*?)$/)
              
              # get line interval
              m, from_line, to_line = *syntax_settings.match(/syntax\(.*? ([0-9]+),([0-9]+)\)/)
              
              # get block name
              m, block_name = *syntax_settings.match(/syntax\(.*?#([0-9a-z_]+)\)/)
              
              code = Kitabu::Markup.content_for({
                :code => code,
                :from_line => from_line,
                :to_line => to_line,
                :block_name => block_name,
                :source_file => source_file
              })
              
              Kitabu::Markup.syntax(code, syntax)
            end
          end
        end
      end
      
      chapter << (parsed_contents + "\n\n")
    end
    
    contents << '<div class="chapter">%s</div>' % chapter
  end

  # save html file
  File.open(html_path, 'w+') do |f|
    f << Kitabu::Base.parse_layout(contents)
  end
end

.generate_pdfObject



127
128
129
# File 'lib/kitabu/base.rb', line 127

def self.generate_pdf
  IO.popen('prince %s -o %s' % [html_path, pdf_path])
end

.html_pathObject



65
66
67
# File 'lib/kitabu/base.rb', line 65

def self.html_path
  KITABU_ROOT + "/output/#{app_name}.html"
end

.layoutsObject



255
256
257
258
259
260
# File 'lib/kitabu/base.rb', line 255

def self.layouts
  @layouts ||= begin
    dir = File.join(GEM_ROOT, "app_generators/kitabu/templates/layouts/")
    Dir.entries(dir).reject{|p| p =~ /^\.+$/ }.sort
  end
end

.parse_layout(contents) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/kitabu/base.rb', line 89

def self.parse_layout(contents)
  template = File.new(template_path).read
  contents, toc = self.table_of_contents(contents)
  cfg = config.merge(:contents => contents, :toc => toc)
  env = OpenStruct.new(cfg)

  ERB.new(template).result env.instance_eval{binding}
end

.pdf_pathObject



69
70
71
# File 'lib/kitabu/base.rb', line 69

def self.pdf_path
  KITABU_ROOT + "/output/#{app_name}.pdf"
end

.syntax?(syntax_name) ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/kitabu/base.rb', line 235

def self.syntax?(syntax_name)
  syntaxes.include?(syntax_name)
end

.syntaxesObject



251
252
253
# File 'lib/kitabu/base.rb', line 251

def self.syntaxes
  Uv.syntaxes
end

.table_of_contents(contents) ⇒ Object



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
# File 'lib/kitabu/base.rb', line 98

def self.table_of_contents(contents)
  return [contents, nil] unless Object.const_defined?('Hpricot') && Object.const_defined?('Unicode')
  
  doc = Hpricot(contents)
  counter = {}

  (doc/"h2, h3, h4, h5, h6").each do |node|
    title = node.inner_text
    permalink = Kitabu::Base.to_permalink(title)
    
    # initialize and increment counter
    counter[permalink] ||= 0
    counter[permalink] += 1
    
    # set a incremented permalink if more than one occurrence
    # is found
    permalink = "#{permalink}-#{counter[permalink]}" if counter[permalink] > 1
    
    node.set_attribute(:id, permalink)
  end
  
  contents = doc.to_html
  io = StringIO.new(contents)
  toc = Toc.new
  REXML::Document.parse_stream(io, toc)

  [contents, toc.to_s]
end

.template_pathObject



73
74
75
# File 'lib/kitabu/base.rb', line 73

def self.template_path
  KITABU_ROOT + "/templates/layout.html"
end

.text_dirObject



81
82
83
# File 'lib/kitabu/base.rb', line 81

def self.text_dir
  KITABU_ROOT + "/text"
end

.theme?(theme_name) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/kitabu/base.rb', line 231

def self.theme?(theme_name)
  themes.include?(theme_name)
end

.themesObject



262
263
264
265
266
267
# File 'lib/kitabu/base.rb', line 262

def self.themes
  @themes ||= begin
    filter = File.join(GEM_ROOT, "app_generators/kitabu/templates/css/*.css")
    Dir[filter].collect{|path| File.basename(path).gsub(/\.css$/, '') }.sort
  end
end


269
270
271
272
273
274
# File 'lib/kitabu/base.rb', line 269

def self.to_permalink(str)
  str = Unicode.normalize_KD(str).gsub(/[^\x00-\x7F]/n,'') 
  str = str.gsub(/[^-_\s\w]/, ' ').downcase.squeeze(' ').tr(' ', '-')
  str = str.gsub(/-+/, '-').gsub(/^-+/, '').gsub(/-+$/, '')
  str
end