Skip to content

Commit 011516b

Browse files
committed
- wiki-styles.css is copied into the export directory and can be overridden
- Wiki export now allows overwriting files (with a warning) - Textile support is dropped (RedCloth wasn't working for me) - The linkifier doesn't nest different page link styles - Page links (for follow or export) always check case of file on filesystem
1 parent f5419c6 commit 011516b

File tree

4 files changed

+92
-20
lines changed

4 files changed

+92
-20
lines changed

textmate-wiki/trunk/Plain Text Wiki.tmbundle/Support/lib/wiki.rb

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
$: << "#{ENV['TM_SUPPORT_PATH']}/lib" if ENV.has_key?('TM_SUPPORT_PATH')
99

1010
class PlainTextWiki
11+
# Default extension
12+
EXT = ".txt"
1113

1214
# set by the initializer, passed in
1315
attr_reader :dir
@@ -23,6 +25,7 @@ def initialize(dir)
2325
end
2426

2527
@dir = dir
28+
@pages = nil
2629
end
2730

2831
def follow_link
@@ -44,7 +47,7 @@ def go_to_index_page
4447
end
4548

4649
def go_to(pagename)
47-
fn = pagename + ".txt"
50+
fn = pagename + EXT
4851

4952
require 'uri'
5053

@@ -69,7 +72,7 @@ def go_to(pagename)
6972

7073
def export_as_html
7174
require 'Find'
72-
require "#{ENV['TM_SUPPORT_PATH']}/lib/redcloth.rb"
75+
require "#{ENV['TM_SUPPORT_PATH']}/lib/bluecloth.rb"
7376
require "#{ENV['TM_SUPPORT_PATH']}/lib/rubypants.rb"
7477

7578
# Ask the user for an export directory, exiting if cancelled
@@ -92,17 +95,28 @@ def export_as_html
9295
end
9396

9497
# Make sure there are no files in the way
98+
files_in_the_way = false
9599
files.each do |source, dest, title|
96100
if File.file?(dest)
97-
puts "There's a file in the way! Please move it before exporting: #{File.split(dest)[1]}"
98-
exit 206
99-
end
101+
files_in_the_way = true
102+
end
103+
end
104+
if File.file?("#{export_dir}/wiki-styles.css")
105+
files_in_the_way = true
106+
end
107+
108+
if files_in_the_way
109+
res = `#{cocoadialog} msgbox --text "Export will replace files" --icon "x" --informative-text "There are files in the way in the export directory. They will be lost if you continue." --button1 "Cancel Export" --button2 "Replace All"`
110+
if res == 1
111+
puts "Cancelled Export Wiki as HTML"
112+
exit 206
113+
end
100114
end
101115

102116
# For each file, HTML-ify the links, convert to HTML using Markdown, and save
103117
files.each do |source, dest, title|
104118
s = with_html_links(open(source, 'r').read)
105-
html = RubyPants.new(RedCloth.new(s).to_html(:markdown, :textile)).to_html
119+
html = RubyPants.new(BlueCloth.new(s).to_html).to_html
106120

107121
File.open(dest, 'w') { |fh|
108122
fh.puts(wiki_header % title)
@@ -111,6 +125,9 @@ def export_as_html
111125
}
112126
end
113127

128+
# Copy the stylesheet over
129+
FileUtils.copy("#{wiki_styles_path}", "#{export_dir}/wiki-styles.css")
130+
114131
# Open the exported wiki in the default HTML viewer
115132
front = File.join(export_dir, "IndexPage.html")
116133
`open #{front}`
@@ -120,6 +137,20 @@ def export_as_html
120137

121138
protected
122139

140+
def pages
141+
@pages ||= load_pages
142+
@pages
143+
end
144+
145+
def load_pages
146+
extensions = [EXT]
147+
all_files = Dir.entries(dir)
148+
all_files.reject! { |fn| File.directory?("#{dir}/#{fn}") }
149+
all_files.reject! { |fn| ! extensions.include?(File.extname(fn)) }
150+
all_files.map! { |fn| fn[0..(fn.length-File.extname(fn).length-1)] }
151+
all_files.sort
152+
end
153+
123154
def templates_dir
124155
"#{ENV['TM_BUNDLE_SUPPORT']}/templates"
125156
end
@@ -133,18 +164,29 @@ def wiki_footer
133164
d = File.file?("#{dir}/wiki-footer.html") ? dir : templates_dir
134165
open("#{d}/wiki-footer.html", "r").read
135166
end
167+
168+
def wiki_styles_path
169+
d = File.file?("#{dir}/wiki-styles.css") ? dir : templates_dir
170+
"#{d}/wiki-styles.css"
171+
end
136172

137173
def with_html_links(s)
138-
s.split("\n").collect { |line|
139-
# markup.other.pagename.camelcase
140-
line.gsub!(/\b([A-Z][a-z]+([A-Z][a-z]*)+)\b/, '<a href="\1.html">\1</a>')
141-
# markup.other.pagename.delimited
142-
line.gsub!(/\[\[(.+)\]\]/) { |m|
143-
pagename = $1.capitalize
144-
"<a href=\"#{pagename}.html\">#{pagename}</a>"
145-
}
146-
line
147-
}.join("\n")
174+
# This match recognises HTML links, and delimited then camelcase
175+
# pagenames. Each is treated differently
176+
# $1: HTML capture
177+
# $2: Delimited capture ($3 is the page name)
178+
# $4: Camelcase capture
179+
s.gsub(/(<a .+<\/a>)|(\[\[(.+)\]\])|(\b([A-Z][a-z]+([A-Z][a-z]*)+)\b)/) { |m|
180+
if $1
181+
$1
182+
else
183+
pagename = $2 ? $2.tr("[]", "").capitalize : $4
184+
if (!pages.include?(pagename)) and (pages.map { |p| p.downcase }.include? pagename.downcase)
185+
pagename = pages.select { |p| p.downcase == pagename.downcase }.first
186+
end
187+
"<a href=\"#{pagename}.html\">#{pagename}</a>"
188+
end
189+
}
148190
end
149191

150192
# public class methods

textmate-wiki/trunk/Plain Text Wiki.tmbundle/Support/templates/wiki-header.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<html>
44
<head>
55
<title>%s</title>
6+
<link rel="stylesheet" href="wiki-styles.css" type="text/css">
67
</head>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
body {
2+
background: #eee;
3+
}
4+
p, ul, ol {
5+
width: 640px;
6+
}
7+
h1, h2, h3, p, blockquote, li {
8+
font-family: Georgia, "Times New Roman", Times, serif;
9+
color: #333;
10+
}
11+
p, li {
12+
font-size: 90%;
13+
line-height: 160%;
14+
}
15+
li li {
16+
font-size: 100%;
17+
line-height: 100%;
18+
}

textmate-wiki/trunk/README.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,23 @@ Usage
3232

3333
Return to the index page at any time: Type shift+ctrl+i
3434

35-
Plain Text Wiki understands Markdown:
35+
36+
Export as HTML
37+
--------------
38+
39+
Choose 'Export Wiki as HTML' from the Plain Text Wiki commands menu. You will
40+
be prompted for a directory where the Web pages will be saved (please make
41+
sure it's empty).
42+
43+
For converting text to HTML, Plain Text Wiki understands Markdown:
3644
http://daringfireball.net/projects/markdown/syntax
3745

38-
If the wiki is written in Markdown, choose 'Export Wiki as HTML' from the
39-
Plain Text Wiki commands menu. You will be prompted for a directory where the
40-
Web pages will be saved (please make sure it's empty).
46+
To customise the HTML of the wiki, add wiki-styles.css to the project
47+
directory--it'll be copied to the export directory and included.
48+
49+
For more control, add wiki-header.html and wiki-footer.html to the project
50+
directory. Include the string "%s" in wiki-header.html to have that replaced
51+
with the page title on export.
4152

4253

4354
Bugs and issues

0 commit comments

Comments
 (0)