Class: Literati::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/literati.rb

Constant Summary collapse

BIRD_TRACKS_REGEX =

Regex used to determine presence of Bird-style comments

/^>(--| )(.*)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, markdowner = MarkdownRenderer) ⇒ Renderer

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).


81
82
83
84
85
# File 'lib/literati.rb', line 81

def initialize(content, markdowner = MarkdownRenderer)
  @bare_content = content
  @markdown = to_markdown
  @markdown_class = markdowner
end

Instance Attribute Details

#markdown_classObject

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



71
72
73
# File 'lib/literati.rb', line 71

def markdown_class
  @markdown_class
end

Instance Method Details

#remove_bird_tracks(line) ⇒ Object

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.



121
122
123
124
# 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) ⇒ Object

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.



134
135
136
137
138
139
140
141
142
143
144
145
146
# 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_htmlObject

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

Returns an HTML string.



152
153
154
# File 'lib/literati.rb', line 152

def to_html
  @markdown_class.new(@markdown).to_html
end

#to_markdownObject

Render the given literate Haskell to a Markdown string.

Returns a Markdown string we can render to HTML.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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