Skip to content

Commit

Permalink
Updates to work in other collections. (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsiever authored Mar 1, 2022
1 parent a6f9c18 commit fbf4ffa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/premonition/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ def generate(site)
end
end

Hooks.register [:documents], :pre_render do |doc|
doc.content = processor.adder(doc.content) if process?(resources, doc)
end
Hooks.register [:pages], :pre_render do |doc|
doc.content = processor.adder(doc.content) if process?(resources, doc)
end
end
end

private

Expand Down
17 changes: 10 additions & 7 deletions lib/premonition/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Jekyll
module Premonition

# Class that does all of the rendering magic within Premonition.
class Processor
def initialize(resources)
Expand All @@ -16,24 +17,26 @@ def initialize(resources)
# content - Markdown content
def adder(content)
o = []
references = load_references(content)
references = ["\n"]
b = nil
is_code_block = false
content.each_line do |l|
is_code_block = !is_code_block if code_block_line?(l)
if is_code_block
o << l
elsif blockquote?(l) && empty_block?(b)
if (m = l.to_s.match(/^\s*\>\s+([a-z]+)\s+\"(.*)\"\s+(\[.*\])?\s*$/i))

if (m = l.match(/^\s*\>\s+([a-z]+)\s+\"(.*)\"\s+(\[.*\])?\s*$/i))
y, t, attrs = m.captures
b = { 'title' => t.strip, 'type' => y.strip.downcase, 'content' => [], 'attrs' => attrs }
else
o << l
end
elsif blockquote?(l) && !empty_block?(b)
b['content'] << l.to_s.match(/^\s*\>\s?(.*)$/i).captures[0]
b['content'] << l.match(/^\s*\>\s?(.*)$/i).captures[0]
else
if !blockquote?(l) && !empty_block?(b)
references = load_references(content)
o << render_block(b, references)
b = nil
end
Expand All @@ -52,15 +55,15 @@ def adder(content)
def load_references(content)
refs = ["\n"]
content.each_line do |l|
unless l.nil? || l == 0 || (l.is_a? String)
refs << l if l.to_s.strip!.match(/^\[.*\]:.*\".*\"$/i)
end
# refs << l if l.strip!.match(/^\[.*\]:.*\".*\"$/i)

refs << l if l.strip.length>0 && l.strip.match(/^\[.*\]:.*\".*\"$/i)
end
refs
end

def code_block_line?(line)
line.strip.start_with?('~~~') || line.strip.start_with?('```')
line.strip.start_with?('~~~')
end

def blockquote?(line)
Expand Down

0 comments on commit fbf4ffa

Please sign in to comment.