My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
#!/bin/bash
# $Id$
#
# Convert the given Markdown text file into print-ready HTML and PDF files using
# the supporting files from the specified style, or the default style if
# unspecified. Markdown source files should ideally use the .md.txt extension.
# The Markdown conversion tool must be installed, and for PDF generation, the
# wkhtmltopdf tool is required (it is skipped, otherwise). New styles can be
# created using the default style (/etc/scripnix/style/default) as a template
# and must be located under the PUBLISH_STYLE_DIR directory. If a style name
# appears within an HTML comment in the first line of the Markdown file, that
# style will be used as the default.
#
# A style directory may contain some or all of the following files:
#
# * header-prefix.html: The initial DOCTYPE, <html>, and <head> elements, ending
# an opening <style> tag.
# * style*.css: One or more CSS files, that are inserted into the HTML in order.
# * header-suffix.html: Begins with the closing </style> tag and includes any
# other <head> elements before closing </head> and opening <body>.
# * footer.html: Includes any final elements before closing </body> and </html>.
# * pre-markdown*.awk: One or more AWK files to process or filter the raw
# Markdown text before it is inserted into the HTML, run in order.
# * post-markdown*.awk: One or more AWK files to process or filter the generated
# HTML document, run in order.
# * suffix: The contents of this file will be inserted between the Markdown
# source filename and the file extension in generated HTML and PDF files.
# * markdown.conf: the command-line switches to use for the markdown command.
# * wkhtmltopdf.conf: the command-line switches to use for the wkhtmltopdf
# command.
#
# Multiple styles may share the same files using links or symlinks. Because
# multiple CSS files and AWK filters are permitted, a style may borrow just a
# selection of another style's files, and even rearrange their order by
# appropriately naming the links.
#
# The optional placeholders @@@TITLE@@@ and @@@YEAR@@@ will be replaced with the
# contents of the document's <h1> element, or the filename if not available, and
# the current 4-digit year, respectively.
#
# This file is a part of Scripnix <http://scripnix.googlecode.com/>.
# Written in 2010 by Dave Rogers <yukondude-strudel-gmail-fullstop-com>.
# This script is released into the Public Domain.

source /usr/local/bin/bin.bash

check_arg_count ${0} ${#} 1 2 '[<style>] <Markdown-text-file>' ${1}

# Lots of things can go wrong, so check 'em all.
if [[ ${#} -eq 1 ]] ; then
markdown_source="${1}"

# Look for a default style in an HTML comment in the first line, or use the
# "default" style if not found.
style=$(sed --quiet --regexp-extended --expression '1s/^(\xef\xbb\xbf)?<!-- ([[:alnum:]]+) -->$/\2/p' "${markdown_source}")
style_dir="${PUBLISH_STYLE_DIR}/${style:-default}"
else
style_dir="${PUBLISH_STYLE_DIR}/${1}"
markdown_source="${2}"
fi

if [[ ! -d "${style_dir}" ]] ; then
echo_err "Style directory ${style_dir} does not exist."
exit 1
fi

if [[ ! -f "${markdown_source}" ]] ; then
echo_err "Markdown text file ${source} does not exist."
exit 1
fi

if [[ ! -x "${MARKDOWN_TO_HTML_COMMAND}" ]] ; then
echo_err "Markdown to HTML converter is not available at ${MARKDOWN_TO_HTML_COMMAND}."
echo_err "Visit http://daringfireball.net/projects/markdown/ to download."
exit 1
fi

markdown_name=$(basename "${markdown_source}" .md.txt)
publish_dir=$(dirname "${markdown_source}")
html_work=$(mktemp)
markdown_work=$(mktemp)
scratch_in=$(mktemp)
scratch_out=$(mktemp)

# Write out the beginning of the HTML header.
if [[ -f "${style_dir}"/header-prefix.html ]] ; then
cat "${style_dir}"/header-prefix.html >>"${html_work}"
fi

# Insert all CSS stylesheet files, in order.
for css in "${style_dir}"/style*.css ; do
cat $css >>"${html_work}"
done

# Write out the rest of the HTML header.
if [[ -f "${style_dir}"/header-suffix.html ]] ; then
cat "${style_dir}"/header-suffix.html >>"${html_work}"
fi

# Strip out any Unicode BOM prefix from the Markdown.
sed --expression '1s/^\xef\xbb\xbf//' "${markdown_source}" >"${markdown_work}"

# Run any optional pre-Markup content filters.
cat "${markdown_work}" >"${scratch_in}"
count=$(ls "${style_dir}"/pre-markdown*.awk 2>/dev/null | wc --lines)

if [[ "${count}" -ne 0 ]] ; then
for awk in "${style_dir}"/pre-markdown*.awk; do
awk --file "${awk}" --re-interval "${scratch_in}" >"${scratch_out}"
cat "${scratch_out}" >"${scratch_in}"
done
fi

cat "${scratch_in}" >"${markdown_work}"

# Write out the converted HTML.
markdown_switches=$(collect_switches "${style_dir}"/markdown.conf)
"${MARKDOWN_TO_HTML_COMMAND}" ${markdown_switches} "${markdown_work}" >>"${html_work}"

# Write out the HTML footer.
if [[ -f "${style_dir}"/footer.html ]] ; then
cat "${style_dir}"/footer.html >>"${html_work}"
fi

# Run any optional post-Markup content filters.
cat "${html_work}" >"${scratch_in}"
count=$(ls "${style_dir}"/post-markdown*.awk 2>/dev/null | wc --lines)

if [[ "${count}" -ne 0 ]] ; then
for awk in "${style_dir}"/post-markdown*.awk; do
awk --file "${awk}" --re-interval "${scratch_in}" >"${scratch_out}"
cat "${scratch_out}" >"${scratch_in}"
done
fi

cat "${scratch_in}" >"${html_work}"

# Use the <h1> text as the title, or the filename if it can't be found. Strip
# out any HTML tags.
title=$(sed --regexp-extended --expression 's/<h1.*?>(.+)<\/h1>/\1/p' --quiet "${html_work}")
title="${title:-$markdown_name}"
title=$(echo "${title}" | sed --regexp-extended --expression 's/<[^>]*>//g')

# Add an optional suffix to the generated filenames.
if [[ -f "${style_dir}"/suffix ]] ; then
suffix=$(cat "${style_dir}"/suffix)
suffix="-${suffix}"
else
suffix=""
fi

# Replace any template title or year tags and write out the converted HTML.
year=$(date +%Y)
sed --regexp-extended --expression "s/@@@TITLE@@@/${title}/g" "${html_work}" |
sed --regexp-extended --expression "s/@@@YEAR@@@/${year}/g" >"${publish_dir}/${markdown_name}${suffix}.html"

rm --force "${html_work}"
rm --force "${markdown_work}"
rm --force "${scratch_in}"
rm --force "${scratch_out}"

if [[ ! -x "${HTML_TO_PDF_COMMAND}" ]] ; then
echo_err "HTML to PDF converter is not available at ${HTML_TO_PDF_COMMAND}."
echo_err "Only HTML has been generated."
exit 0
fi

# Convert generated HTML to PDF. The eval is needed to deal with quotes that
# might appear in wkhtmltopdf_switches.
wkhtmltopdf_switches=$(collect_switches "${style_dir}"/wkhtmltopdf.conf)
eval "${HTML_TO_PDF_COMMAND} ${wkhtmltopdf_switches} \"${publish_dir}/${markdown_name}${suffix}.html\" \"${publish_dir}/${markdown_name}${suffix}.pdf\""

Change log

r140 by yukondude on Oct 22, 2010   Diff
Remove (most) hardcoded references to
wkhtmltopdf.
Go to: 
Sign in to write a code review

Older revisions

r139 by yukondude on Oct 22, 2010   Diff
Look for a default style on the first
line of the Markdown source.
r138 by yukondude on Oct 12, 2010   Diff
Strip out any HTML tags that happened
to sneak into the title.
r137 by yukondude on Oct 10, 2010   Diff
Pull out the title regardless of
attributes in the <h1> tag.
All revisions of this file

File info

Size: 6785 bytes, 171 lines

File properties

svn:executable
*
svn:keywords
Id
Powered by Google Project Hosting