In Files

Parent

Literati::Renderer

Constants

BIRD_TRACKS_REGEX

Regex used to determine presence of Bird-style comments

Attributes

markdown_class[RW]

The Markdown class we're using to render HTML; is our RedCarpet wrapped by default.

Public Class Methods

new(content, markdowner = MarkdownRenderer) click to toggle source

Initialize a new literate Haskell renderer.

content - The literate Haskell code string markdowner - The class we'll use to render the HTML (defaults

to our RedCarpet wrapper).
# File lib/literati.rb, line 81
def initialize(content, markdowner = MarkdownRenderer)
  @bare_content = content
  @markdown = to_markdown
  @markdown_class = markdowner
end

Public Instance Methods

remove_bird_tracks(line) click to toggle source

Remove Bird-style comment markers from a line of text.

comment = "> Haskell codes"
remove_bird_tracks(comment)
# => "Haskell codes"

Returns the given line of text sans bird tracks.

# File lib/literati.rb, line 121
def remove_bird_tracks(line)
  tracks = line.scan(BIRD_TRACKS_REGEX)[0]
  (tracks.first == " ") ? tracks[1] : tracks.join
end
slurp_remaining_bird_tracks(lines) click to toggle source

Given an Array of lines, pulls from the front of the Array until the next line doesn't match our bird tracks regex.

lines = ["> code", "> code", "", "not code"]
slurp_remaining_bird_tracks(lines)
# => "code\ncode"

Returns the lines mashed into a string separated by a newline.

# File lib/literati.rb, line 134
def slurp_remaining_bird_tracks(lines)
  tracked_lines = []

  while lines.first =~ BIRD_TRACKS_REGEX
    tracked_lines << remove_bird_tracks(lines.shift)
  end

  if tracked_lines.empty?
    ""
  else
    "\n" + tracked_lines.join("\n")
  end
end
to_html() click to toggle source

Render the Markdown string into HTML using the previously specified Markdown renderer class.

Returns an HTML string.

# File lib/literati.rb, line 152
def to_html
  @markdown_class.new(@markdown).to_html
end
to_markdown() click to toggle source

Render the given literate Haskell to a Markdown string.

Returns a Markdown string we can render to HTML.

# File lib/literati.rb, line 90
def to_markdown
  lines = @bare_content.split("\n")
  markdown = ""

  # Using `while` here so we can alter the collection at will
  while current_line = lines.shift
    # If we got us some of them bird tracks...
    if current_line =~ BIRD_TRACKS_REGEX
      # Remove the bird tracks from this line
      current_line = remove_bird_tracks(current_line)
      # Grab the remaining code block
      current_line << slurp_remaining_bird_tracks(lines)

      # Fence it and add it to the output
      markdown << "```haskell\n#{current_line}\n```\n"
    else
      # No tracks?  Just stick it back in the pile.
      markdown << current_line + "\n"
    end
  end

  markdown
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.