Class: Mdexport

Inherits:
Object
  • Object
show all
Defined in:
lib/mdexport.rb

Class Method Summary collapse

Class Method Details

.runObject



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
198
199
200
201
# File 'lib/mdexport.rb', line 7

def self.run

files = []
files += Dir["*.md"]
files += Dir["*.markdown"]

if files.size == 0
puts "There is no markdown files here"
end

output_dir = "html_files"

files.each do |file|

extension = File.extname(file)
basename = File.basename(file, extension)
content = File.read file
html_body = GitHub::Markdown.render_gfm content
html_title = basename
html_filename = output_dir + "/" + basename + ".html"

html_header = "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">\n<style>\nh1,\nh2,\nh3,\nh4,\nh5,\nh6,\np,\nblockquote {\n  margin: 0;\n  padding: 0;\n}\nbody {\n  font-family: \"Helvetica Neue\", Helvetica, \"Hiragino Sans GB\", Arial, sans-serif;\n  font-size: 13px;\n  line-height: 18px;\n  color: #737373;\n  background-color: white;\n  margin: 10px 13px 10px 13px;\n}\ntable {\nmargin: 10px 0 15px 0;\nborder-collapse: collapse;\n}\ntd,th { \nborder: 1px solid #ddd;\npadding: 3px 10px;\n}\nth {\npadding: 5px 10px;  \n}\n\na {\n  color: #0069d6;\n}\na:hover {\n  color: #0050a3;\n  text-decoration: none;\n}\na img {\n  border: none;\n}\np {\n  margin-bottom: 9px;\n}\nh1,\nh2,\nh3,\nh4,\nh5,\nh6 {\n  color: #404040;\n  line-height: 36px;\n}\nh1 {\n  margin-bottom: 18px;\n  font-size: 30px;\n}\nh2 {\n  font-size: 24px;\n}\nh3 {\n  font-size: 18px;\n}\nh4 {\n  font-size: 16px;\n}\nh5 {\n  font-size: 14px;\n}\nh6 {\n  font-size: 13px;\n}\nhr {\n  margin: 0 0 19px;\n  border: 0;\n  border-bottom: 1px solid #ccc;\n}\nblockquote {\n  padding: 13px 13px 21px 15px;\n  margin-bottom: 18px;\n  font-family:georgia,serif;\n  font-style: italic;\n}\nblockquote:before {\n  content:\"\\201C\";\n  font-size:40px;\n  margin-left:-10px;\n  font-family:georgia,serif;\n  color:#eee;\n}\nblockquote p {\n  font-size: 14px;\n  font-weight: 300;\n  line-height: 18px;\n  margin-bottom: 0;\n  font-style: italic;\n}\ncode, pre {\n  font-family: Monaco, Andale Mono, Courier New, monospace;\n}\ncode {\n  background-color: #fee9cc;\n  color: rgba(0, 0, 0, 0.75);\n  padding: 1px 3px;\n  font-size: 12px;\n  -webkit-border-radius: 3px;\n  -moz-border-radius: 3px;\n  border-radius: 3px;\n}\npre {\n  display: block;\n  padding: 14px;\n  margin: 0 0 18px;\n  line-height: 16px;\n  font-size: 11px;\n  border: 1px solid #d9d9d9;\n  white-space: pre-wrap;\n  word-wrap: break-word;\n}\npre code {\n  background-color: #fff;\n  color:#737373;\n  font-size: 11px;\n  padding: 0;\n}\nsup {\n  font-size: 0.83em;\n  vertical-align: super;\n  line-height: 0;\n}\n* {\n-webkit-print-color-adjust: exact;\n}\n@media screen and (min-width: 914px) {\n  body {\n      width: 854px;\n      margin:10px auto;\n  }\n}\n@media print {\nbody,code,pre code,h1,h2,h3,h4,h5,h6 {\n  color: black;\n}\ntable, pre {\n  page-break-inside: avoid;\n}\n}\n</style>\n<title>\#{html_title}</title>\n\n</head>\n<body>\n"

html_footer = "</body>\n</html>\n"

html_content = html_header + html_body + html_footer
FileUtils.mkdir(output_dir) unless File.exist?(output_dir)
FileUtils.rm(html_filename) if File.exist?(html_filename)
File.write(html_filename, html_content)

end # end files.each

end