forked from houshuang/folders2web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-scrivener-dokuwiki.rb
58 lines (48 loc) · 1.67 KB
/
convert-scrivener-dokuwiki.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# encoding: UTF-8
# remember to compile with Transformations...Convert Plain Text to Paragraph Spacing
# small script to convert my style of writing markdown(ish) in Scrivener to dokuwiki
# allows you to use - with no indent instead of * indented two spaces for bullet list
# (can use tabs to increase indent)
# b. at the beginning of a line wraps line in blockquote
# uses h1. h2. for headings with DW plugin, can be modified to support classic DW headers
class String
def gsubs!(*searches)
self.replace(gsubs(*searches))
end
def gsubs(*searches)
if searches[0].kind_of?(Hash)
args = searches.shift
all_replace = try { args[:all_with] }
end
tmp = self.dup
searches.each do |search|
if all_replace
tmp.gsub!(search, all_replace)
else
tmp.gsub!(search[0], search[1])
end
end
return tmp
end
end
# a few extra file functions
class File
class << self
# adds File.write - analogous to File.read, writes text to filename
def write(filename, text)
File.open(filename,"w") {|f| f << text}
end
end
end
a = File.read(ARGV[0])
a.gsubs!(
[/([^\n])\n([^\n])/m, '\1' + "\n\n" + '\2'], # convert to paragraph spacing
[/.(h[1-9]\.)/, '\1'], # clean up spaces before titles
[/\n\n\n+/, "\n\n"], # remove extraneous linespacing
[/^b.(.+?)$/, '<blockquote>\1</blockquote>']
)
# convert bullet lists with - and tabs to * and spaces
a.gsub!(/^(\t*)- /) { |f| " " + f.gsubs([/\t/, ' '], ["- ", "* "]) }
# no double-spacing between bullet items
a.gsub!(/\*(.+?)\n\n[^ ]/m) {|f| f.gsub("\n\n", "\n")[0..-2] + "\n" + f[-1] }
File.write(ARGV[0]+".txt", a)