Skip to content
Newer
Older
100644 112 lines (92 sloc) 2.96 KB
cf8a67b @steveklabnik Bye bye, octopress.
authored
1 # about 3/4ths of this is based on https://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rb
2
3 # https://github.com/tanoku/redcarpet/pull/91
4 class StripDown < Redcarpet::Render::Base
5 # Methods where the first argument is the text content
6 [
7 # block-level calls
8 :block_code, :block_quote,
9 :block_html, :header, :list,
10 :list_item, :paragraph,
11
12 # span-level calls
13 :autolink, :codespan, :double_emphasis,
14 :emphasis, :raw_html, :triple_emphasis,
15 :strikethrough, :superscript,
16
17 # low level rendering
18 :entity, :normal_text
19 ].each do |method|
20 define_method method do |*args|
21 args.first
22 end
23 end
24
25 # Other methods where the text content is in another argument
26 def link(link, title, content)
27 content
28 end
29 end
30
31 class RelRenderer < Redcarpet::Render::HTML
32 attr_accessor :outline
33
34 def initialize
35 @outline = []
36 super
37 end
38
39 def link(link, title, content)
40 "<a href='#{link}' rel='#{title}'>#{content}</a>"
41 end
42
43 def header(text, header_level)
44 text_slug = text.gsub(/\W/, "_").downcase
45
46 self.outline << [header_level, "<a href='##{text_slug}'>#{text}</a>"]
47
48 "<h#{header_level} id='#{text_slug}'>#{text}</h#{header_level}>"
49 end
50 end
51
52 class SourceFile
53 attr_accessor :content
54 attr_accessor :metadata
55 attr_accessor :outline
56
57 def self.archive_list
58 @archive_list ||= Dir.glob("posts/*.{markdown,md}").sort.reverse.collect do |filename|
59 content = File.read(filename)
60 content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
61 title = YAML.load($1)["title"]
62 [title, "/#{filename.gsub(/\..*?$/, "")}"]
63 end
64 end
65
66 def self.archive_data
67 @archive_data ||= Dir.glob("posts/*.{markdown,md}").sort.reverse.collect do |filename|
68 content = File.read(filename)
69 content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
70 title = YAML.load($1)["title"]
71 r = Redcarpet::Markdown.new(StripDown, :fenced_code_blocks => true)
72 summary = r.render($')[0,200] + "..."
73 filename =~ /\/(\d\d\d\d-\d\d-\d\d)/
74 updated = Date.strptime($1).to_datetime.rfc2822
75 ["http://blog.steveklabnik.com/#{filename.gsub(/\..*?$/, "")}", title, updated, summary]
76 end
77 end
78
79 def self.last_updated
80 filename = Dir.glob("posts/*.{markdown,md}").sort.reverse.first
81 filename =~ /\/(\d\d\d\d-\d\d-\d\d)/
82 Date.strptime($1).to_datetime.rfc2822
83 end
84
85 def initialize(name)
86 base = "posts"
87 content = ""
88
89 # oh god this is bad.
90 begin
91 content = File.read(File.join(base, "#{name}.markdown"))
92 rescue
93 content = File.read(File.join(base, "#{name}.md"))
94 end
95
96 if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
97 renderer = RelRenderer.new
98 r = Redcarpet::Markdown.new(renderer, :fenced_code_blocks => true)
99 self.content = r.render($')
100 self.outline = renderer.outline
101
102 begin
103 self.metadata = YAML.load($1)
104 rescue => e
105 puts "YAML Exception reading #{name}: #{e.message}"
106 end
107 end
108
109 self.metadata ||= {}
110 end
111 end
Something went wrong with that request. Please try again.