Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Soryu committed Mar 5, 2010
0 parents commit 4de672b
Show file tree
Hide file tree
Showing 10 changed files with 2,554 additions and 0 deletions.
132 changes: 132 additions & 0 deletions Commands/Generate.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>saveActiveFile</string>
<key>command</key>
<string>#!/usr/bin/env ruby -s
require 'erb'
def markdown_text (text)
text.gsub(/`(.*?)`/, '&lt;code&gt;\1&lt;/code&gt;')
end
def make_id (text)
text = text.gsub(/`(.*?)`/, '\1')
text.gsub!(/[- \/_]+/, '_')
text.gsub!(/[^a-zA-Z0-9_]+/, '')
text.downcase
end
Dir.chdir(ENV['TM_PROJECT_DIRECTORY'])
$toc = [ "&lt;h1&gt;Table of Contents&lt;/h1&gt;" ]
$chapters = [ ]
$toc_template = "templates/toc.rhtml" if $toc_template.nil?
$chapter_template = "templates/chapter.rhtml" if $chapter_template.nil?
$all_template = "templates/all.rhtml" if $all_template.nil?
$output_dir = "html" if $output_dir.nil?
files = Dir.entries("pages").sort.grep(/.*\.markdown/)
files.each_with_index do |file, i|
def html_link (markdown_file)
markdown_file.sub(/^(?:\d+ )?(.*?)\.markdown$/, '\1.html')
end
puts "• Convert #{file}…"
$page_toc = [ ]
IO.popen('"' + ENV['TM_BUNDLE_SUPPORT'] + '/bin/MultiMarkdown.pl"| "' + ENV['TM_SUPPORT_PATH'] + '/bin/SmartyPants.pl"| "' + ENV['TM_BUNDLE_SUPPORT'] + '"/add_image_size.rb', "r+") do |f|
Thread.fork do
heading_stack = [ i ]
lastlevel = 0;
File.open("pages/#{file}").each_line do |line|
if(m = line.match(/^(#+)\s+(.*)$/))
level = m[1].length
title = m[2]
if heading_stack.length &lt; level
heading_stack.push(0)
# $toc.push("&lt;ol&gt;")
else
while heading_stack.length &gt; level
heading_stack.pop
# $toc.push("&lt;/ol&gt;")
end
end
heading_stack.push(heading_stack.pop + 1)
prefix = heading_stack.join(".")
puts(" #{prefix} #{title}")
toc_entry = "#{"&lt;ul&gt;&lt;li&gt;" if(level &gt; lastlevel)}#{"&lt;/li&gt;&lt;li&gt;" if(level == lastlevel)}#{"&lt;/li&gt;&lt;/ul&gt;" * (lastlevel - level) if(level &lt; lastlevel)}&lt;span class='toc_number'&gt;#{prefix}&lt;/span&gt;&lt;a href='#{html_link file}##{make_id title}'&gt;#{markdown_text title}&lt;/a&gt;";
$toc.push(toc_entry)
$page_toc.push(toc_entry)
f.write("#{m[1]} &lt;span id='#{make_id title}'&gt;#{prefix}&lt;/span&gt; #{title}")
lastlevel = level;
else
f.write(line)
end
end
lastlevel.times {$page_toc.push("&lt;/li&gt;&lt;/ul&gt;")}
begin
f.write("\n"+File.open("markdown.references").read)
rescue
end
f.close_write
lastlevel.times {$toc.push("&lt;/li&gt;&lt;/ul&gt;")}
# heading_stack.length.times { $toc.push("&lt;/ol&gt;") }
end
$page = { :title =&gt; %x{ grep '^#' 'pages/#{file}'|head -n1 }.sub(/^#+\s+(.*)\n/, '\1'), :content =&gt; f.read, :name =&gt; "chapter", :page_toc =&gt; $page_toc.join("\n") }
$prev = i &gt; 0 ? { :link =&gt; "#{html_link files[i-1]}", :title =&gt; markdown_text(%x{ grep '^#' 'pages/#{files[i-1]}'|head -n1 }.sub(/^#+\s+(.*)\n/, '\1')) } : nil
$next = i + 1 &lt; files.length ? { :link =&gt; "#{html_link files[i+1]}", :title =&gt; markdown_text(%x{ grep '^#' 'pages/#{files[i+1]}'|head -n1 }.sub(/^#+\s+(.*)\n/, '\1')) } : nil
File.open("#{$output_dir}/#{html_link file}", "w") do |f|
f.write(ERB.new(File.open($chapter_template), 0, '&lt;&gt;').result)
end
$chapters.push($page)
end
end
$alltoc = { :toc =&gt; $toc.join("\n") }
File.open("#{$output_dir}/all_pages.html", "w") do |f|
f.write(ERB.new(File.open($all_template), 0, '&lt;&gt;').result)
end
File.open("#{$output_dir}/index.html", "w") do |f|
$page = { :content =&gt; $toc.join("\n"), :name =&gt; "toc", :title =&gt; "Table of Contents" }
f.write(ERB.new(File.open($toc_template), 0, '&lt;&gt;').result)
end
</string>
<key>input</key>
<string>none</string>
<key>keyEquivalent</key>
<string>^G</string>
<key>name</key>
<string>Generate</string>
<key>output</key>
<string>showAsTooltip</string>
<key>scope</key>
<string>text.html.markdown</string>
<key>uuid</key>
<string>C445442D-06BF-4CEE-A642-6209B84632A6</string>
</dict>
</plist>
22 changes: 22 additions & 0 deletions Commands/Open Generated Stuff.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>open "$TM_PROJECT_DIRECTORY/html/index.html"</string>
<key>input</key>
<string>none</string>
<key>keyEquivalent</key>
<string>^O</string>
<key>name</key>
<string>Open Generated Files</string>
<key>output</key>
<string>discard</string>
<key>scope</key>
<string>text.html.markdown</string>
<key>uuid</key>
<string>B3AF90C8-A1B0-4583-A5E4-74374B68B015</string>
</dict>
</plist>
21 changes: 21 additions & 0 deletions Commands/Readme.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>. "$TM_SUPPORT_PATH/lib/webpreview.sh"
html_header "Markdown2Book Help file" "Markdown2Book"
Markdown.pl "$TM_BUNDLE_SUPPORT/help.markdown"|SmartyPants.pl
html_footer</string>
<key>input</key>
<string>none</string>
<key>name</key>
<string>Readme</string>
<key>output</key>
<string>showAsHTML</string>
<key>uuid</key>
<string>B9B57AA0-0024-41F6-B6AE-C60FCDC1E8B1</string>
</dict>
</plist>
34 changes: 34 additions & 0 deletions Commands/Show References.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>res=$(CocoaDialog dropdown --text "References" --items `cat "$TM_PROJECT_DIRECTORY/markdown.references"|sed 's/ /_/'` --button1 "Insert")
[[ $(head -n1 &lt;&lt;&lt;"$res") == "2" ]] &amp;&amp; exit_discard
res=$(tail -n1 &lt;&lt;&lt;"$res")
let "res += 2"
res=$(head -n "$res" "$TM_PROJECT_DIRECTORY/markdown.references" |tail -n 1)
res=$(sed 's/\[\(.*\)\].*/[${1:Reference text}][\1]/'&lt;&lt;&lt;"$res")
echo -n "$res"</string>
<key>input</key>
<string>none</string>
<key>keyEquivalent</key>
<string>^l</string>
<key>name</key>
<string>Show References</string>
<key>output</key>
<string>insertAsSnippet</string>
<key>scope</key>
<string>text.html.markdown</string>
<key>uuid</key>
<string>3E0EDBF3-2018-46EA-95EE-C4EB20118789</string>
</dict>
</plist>
43 changes: 43 additions & 0 deletions Commands/Update References.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/bin/bash
cd "$TM_PROJECT_DIRECTORY"
if [ ! -f "markdown.references" ]; then touch "markdown.references"; fi
all_refs=$(grep -ho '\[[^]]*\]\[[^]]*\]' pages/*.markdown|perl -pe 's/.*\[(.*)\]$/$1/'|sort|uniq)
known_refs=$(grep '^\[.*\]' markdown.references|perl -pe 's/^\[(.*?)\].*/$1/')
all_ptrn=$(tr &lt;&lt;&lt;"$all_refs" '\n' '|')
known_ptrn=$(tr &lt;&lt;&lt;"$known_refs" '\n' '|')
all_ptrn="${all_ptrn:0:${#all_ptrn}-1}"
known_ptrn="${known_ptrn:0:${#known_ptrn}-1}"
echo -e "Missing:\n"
grep -Ev &lt;&lt;&lt;"$all_refs" "^($known_ptrn)$"|perl -pe 's/.*/[$&amp;]: MISSING/'|tee -a markdown.references
echo -e "\nRedundant:\n"
grep -Ev &lt;&lt;&lt;"$known_refs" "^($all_ptrn)$"
</string>
<key>input</key>
<string>none</string>
<key>keyEquivalent</key>
<string>^U</string>
<key>name</key>
<string>Update References</string>
<key>output</key>
<string>showAsTooltip</string>
<key>scope</key>
<string>text</string>
<key>uuid</key>
<string>749EA16A-700B-43B3-9457-FA12D8301CAE</string>
</dict>
</plist>
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This TextMate bundle is for compiling a Documentation project made of several Markdown files to HTML. It generates a TOC, a print version with all chapters on one page and HTML files for each chapter. You can use references to easily link between your pages.
12 changes: 12 additions & 0 deletions Support/add_image_size.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env ruby

STDIN.each_line do |line|
line.gsub!(/<img(?![^>]*?(?:width|height)=)[^>*]src="(.*?)"/) do |tag|
size = %x(sips -g pixelWidth -g pixelHeight "#{$1}")
size.gsub(/pixel(Width|Height): (\d+)/) { |str| tag += " #{$1.downcase}=\"#{$2}\"" }
# use the next line to change the image path
# tag.gsub(/src="([^"]+)"/, 'src="http://yoursite.com/path/to/images/\1"')
tag.gsub(/src="([^"]+)"/, 'src="\1"')
end
print line
end
Loading

0 comments on commit 4de672b

Please sign in to comment.