Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add basic TOC generation.

  • Loading branch information...
commit afd38cc13fe47ac50338aa5d44b42e8aa2186b04 1 parent 854e473
@jarib jarib authored
Showing with 26 additions and 10 deletions.
  1. +25 −9 Rakefile
  2. +1 −1  pages/ofte-stilte-sporsmal.md
View
34 Rakefile
@@ -5,29 +5,45 @@ directory 'build'
CLEAN << 'build'
module Hdo
- class MarkdownRenderer < Redcarpet::Render::HTML
+ def self.id_for(str)
+ str.gsub(/\s+/, '-').gsub(/[^\w-]/, '').downcase
+ end
+
+ class DefaultRenderer < Redcarpet::Render::HTML
def header(str, level)
- id = str.gsub(/\s+/, '-').gsub(/[^\w-]/, '').downcase
- "<h#{level} id='#{id}'>#{str}</h#{level}>"
+ "<h#{level} id='#{Hdo.id_for str}'>#{str}</h#{level}>\n"
+ end
+ end
+
+ class TocRenderer < Redcarpet::Render::HTML_TOC
+ def header(str, level)
+ "<li><a href='#{Hdo.id_for str}'>#{str}</a></li>\n"
end
end
end
task :default => :clean do
- renderer = Hdo::MarkdownRenderer.new(with_toc_data: true)
- markdown = Redcarpet::Markdown.new(renderer,
+ markdown = Redcarpet::Markdown.new(Hdo::DefaultRenderer.new,
autolink: true,
space_after_headers: true,
tables: true,
fenced_code_blocks: true
)
+ toc = Redcarpet::Markdown.new(Hdo::TocRenderer.new)
+
FileList['pages/**/*.md'].each do |source|
- dest = source.gsub(/pages\/(.+)\.md/, 'build/\1.html')
- mkdir_p File.dirname(dest), verbose: false
+ base = source.gsub(/pages\/(.+)\.md/, 'build/\1')
+
+ mkdir_p File.dirname(base), verbose: false
+
+ html_path = "#{base}.html"
+ toc_path = "#{base}.toc.html"
- open(dest, "w") { |file| file << markdown.render(File.read(source)) }
+ open(html_path, "w") { |file| file << markdown.render(File.read(source)) }
+ open(toc_path, "w") { |file| file << toc.render(File.read(source)) }
- puts "#{source} => #{dest}"
+ puts "#{source} => #{html_path}"
+ puts "#{source} => #{toc_path}"
end
end
View
2  pages/ofte-stilte-sporsmal.md
@@ -1,7 +1,7 @@
-
# Ofte stilte spørsmål
### Har dette blitt gjort før?
+
Ikke i Norge. Derimot er lignende initiativ på fremmarsj i resten av verden. Lignende initiativ er på plass eller under utvikling i flere land, inkludert USA, Storbritannia, Frankrike, Polen og Finland.
### Har dere en faglig inspirasjonskilde?
Please sign in to comment.
Something went wrong with that request. Please try again.