-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblog
executable file
·198 lines (172 loc) · 4.48 KB
/
blog
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env ruby
# encoding: UTF-8
# Author: Anders Janmyr
require "rubygems"
require 'optparse'
require 'ostruct'
%w(redcarpet htmlentities).each do |lib|
begin
require lib
rescue Exception => err
puts "Missing gem #{lib}, install with `gem install #{lib}"
end
end
# This is the name of the script that is called
PROGRAM_NAME = $0
PROGRAM_VERSION = 1.0
# Create an OpenStruct to save the options.
def options
@options ||= OpenStruct.new
end
# This is the options of the program, see OptionParser
# http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html
def program_options
[
# The values of the array are,
# [long_option, short_option and parameter, description, code to execute]
['--google', '-g', "Format for Google blogger.",
lambda { |value| options.google = true }
],
['--jayway', '-j', "Format for Jayway blog.",
lambda { |value| options.jayway = true }
],
['--utf', '-u', "Include meta charset utf8",
lambda { |value| options.utf = true }
],
['--stylesheet', '-s', "Add a stylesheet, md.css",
lambda { |value| options.stylesheet = true }
],
['--verbose', '-v', "Log to standard output.",
lambda { |value| options.verbose = true }
],
['--version', '-V', "Display the program version.",
lambda { |value|
puts "#{program_name}, version #{PROGRAM_VERSION}"
exit
}
]
]
end
option_parser = OptionParser.new do |opts|
opts.banner = "#{PROGRAM_NAME} [options] files..."
opts.separator ""
opts.separator "Options are ..."
# Add the command on_tail, to make it appear as the last option in the list.
opts.on_tail("-h", "--help", "-H", "Display this help message.") do
puts opts
exit
end
program_options.each { |args| opts.on(*args) }
end
begin
# Parse the options and remove them from the ARGV array
option_parser.parse!
rescue OptionParser::ParseError => error
puts error.message
puts option_parser
exit
end
class Array
def split(str)
result = []
group = []
each do |item|
if item.include?(str)
result << group
group = []
else
group << item
end
end
result << group
result
end
end
Chunk = Struct.new(:lang, :source, :options)
def highlight(chunk)
if options.jayway
text = "<pre>\n#{chunk.source}\n</pre>\n"
else
text = highlight_code(chunk)
end
if options.google
text = text.sub(/(.*)(<pre>.*<\/pre>)(.*)/mi, '<blockquote>\2</blockquote>')
end
puts text
end
def highlight_code(chunk)
tempfile = 'tempfile.txt'
f = File.open(tempfile, "w") do |f|
f.puts "#{chunk.source}"
end
%x{source-highlight --src-lang #{chunk.lang} < #{tempfile}}
ensure
File.delete(tempfile) if File.exist?(tempfile)
end
def convert_texts(chunks)
chunks.each do |chunk|
if chunk.lang == "markdown"
render_markdown chunk
else
highlight(chunk)
end
end
end
$markdown = Redcarpet::Markdown.new(
Redcarpet::Render::HTML, :smart => true, :autolink => true, :tables => true)
$html_entities = HTMLEntities.new
def render_markdown chunk
if chunk.options && chunk.options[:html]
text = chunk.source
else
text = $html_entities.encode(chunk.source, :basic)
end
puts $markdown.render(text)
end
def load_from_file(filename, snippet)
path = File.expand_path(filename)
raise "No such file error: " + path unless File.exist?(path)
snippet_file = File.open(path)
lines = snippet_file.readlines
return lines.join if snippet.nil?
before, between, *rest = lines.split(snippet.chomp)
return between.join
end
def handle_file file
chunks = []
lang = "markdown"
str = ""
chunk_options = {}
file.each_line do |line|
if line =~ /^```/
chunks << Chunk.new(lang, str, chunk_options)
chunk_options = {}
line = line[3..-1]
lang, filename, snippet = line.split
lang = 'markdown' unless lang
if filename && filename != 'html'
str = load_from_file(filename, snippet)
chunks << Chunk.new(lang, str, chunk_options)
chunk_options = {}
lang = "markdown" # reset the lang
end
chunk_options[:html] = true if filename == 'html'
str = ""
else
str << line
end
end
chunks << Chunk.new(lang, str, chunk_options)
convert_texts(chunks)
end
puts '<meta charset="utf-8">' if options.utf
puts '<link href="md.css" rel="stylesheet" type="text/css">' if options.stylesheet
if ARGV.empty?
handle_file STDIN
else
ARGV.each do |arg|
file = File.open(arg)
handle_file file
file.close
end
end