lib/blogit/parsers/markdown_parser.rb in blogit-1.0.0.rc1 vs lib/blogit/parsers/markdown_parser.rb in blogit-1.0.0
- old
+ new
@@ -1,25 +1,49 @@
class Blogit::Parsers::MarkdownParser
require "nokogiri"
require "albino"
require "blogit/renderers"
-
+
+ # A String containing the content to be parsed
+ attr_reader :content
+
def initialize(content)
@content = content
end
+ # The parsed content
+ #
+ # Returns an HTML safe String
def parsed
ensure_pygments_is_installed if Blogit::configuration.highlight_code_syntax
-
- renderer = Blogit::configuration.highlight_code_syntax ? Redcarpet::Render::HTMLWithAlbino : Redcarpet::Render::HTML
- markdown = Redcarpet::Markdown.new(renderer, Blogit.configuration.redcarpet_options)
- html_content = markdown.render(@content).html_safe
+ markdown.render(content).html_safe
end
+
private
+
+ # The Redcarpet renderer to use
+ def renderer
+ if Blogit::configuration.highlight_code_syntax
+ Redcarpet::Render::HTMLWithAlbino
+ else
+ Redcarpet::Render::HTML
+ end
+ end
+
+ # The Redcarpet Markdown handler
+ def markdown
+ @markdown ||= Redcarpet::Markdown.new(renderer,
+ Blogit.configuration.redcarpet_options)
+ end
+
+
+ # Ensures pygments is installed
+ #
+ # Raises StandardError if pygments is not available on this machine
def ensure_pygments_is_installed
warning = <<-WARNING
[blogit] The pygmentize command could not be found in your load path!
Please either do one of the following:
@@ -32,9 +56,10 @@
WARNING
raise warning unless which(:pygmentize)
end
# Check if an executable exists in the load path
+ #
# Returns nil if no executable is found
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|