BashRC: Improved the md2html command.

Signed-off-by: Hermann Mayer's avatarHermann Mayer <hermann.mayer92@gmail.com>
parent 12a82421
......@@ -56,166 +56,112 @@ function md5check()
function md2html()
{
local TOC="01_aaatoc.md"
local TMP="__index.tmp"
local OUT="index.html"
local TITLE=$(basename `pwd`)
echo > "${TMP}"
echo > "${OUT}"
cat << 'EOF' > "${TOC}"
# Table of contents
local TOC=$(mktemp)
local CNT=$(mktemp)
local FCNT=$(mktemp)
local OUT="$(pwd)/index.html"
# Generate table of contents
grep -P '^#' *.md | cut -d ':' -f2 \
| ruby -n -e '
match = /([#]+) /
line = $_.strip
level = line.scan(match).flatten.first.size * 2 - 2
slug = line.split("#").reject(&:empty?).join.strip
.gsub(/[^a-z0-9 -]/i, "").gsub(/\s+/, " ").gsub(/\W/, "-")
.downcase
puts line.gsub(match, "#{" " * level}* [").concat("](##{slug})")
' > "${TOC}"
# Generate concatenated page contents
for FILE in *.md; do
(echo; cat "${FILE}"; echo) >> "${CNT}"
done
# Place anchors to all headlines and add table of contents
ruby >"${FCNT}" <<EOR
toc = File.read('${TOC}').lines.yield_self do |lines|
lines.slice(1..).join if lines.first.match?(/^\* /)
end
last_level = nil
shrink = false
toc = toc.lines.map do |line|
level = line.scan(/(\s+)\*/).flatten.first&.size || 0
shrink = true if last_level.nil? && !level.zero?
shrink = false if level.zero?
line = line.slice(2..) if shrink
last_level = level
line
end.join
toc.prepend("\n").prepend('<a name="table-of-contents"></a>')
toc = "\n#{toc}\n"
contents = File.read('${CNT}')
first_head = contents.lines.grep(/^# /).first.strip
prev_head = nil
lines = contents.lines.map do |line|
next(line) unless line.match? /^([#]+) /
prefix = prev_head == first_head ? toc : ''
slug = line.split("#").reject(&:empty?).join.strip
.gsub(/[^a-z0-9 -]/i, "").gsub(/\s+/, " ").gsub(/\W/, "-")
.downcase
prev_head = line.strip
line.prepend("#{%Q(<a name="#{slug}"></a>)}\n").prepend(prefix)
end.join
lines.prepend(toc) unless lines.include?(toc)
puts lines
EOR
# Get the title
local TITLE=$(grep '^# ' "${FCNT}" | head -n1 | cut -d' ' -f2-)
cat >"${OUT}" <<EOF
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
EOF
grep -P '^#' *.md | cut -d ':' -f2 | \
sed 's/#/* /g' | \
sed 's/\* \* \* \*/ */g' | \
sed 's/\* \* \*/ */g' | \
sed 's/\* \*/ */g' | \
sed 's/\* /\* [/g' | \
sed 's/$/]/g' | \
node -e "
var fs = require('fs');
var chunks = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
chunks += chunk;
});
process.stdin.on('end', function() {
var tmp = '# Table of contents\n\n';
chunks.split('\n').forEach(function(line) {
if ('' == line) {
return;
}
var slug = line.split('*');
slug.shift();
slug = slug.join('').trim().split(' ').join('-');
slug = slug.replace(/\(/g, '')
.replace(/\./g, '-')
.replace(/\,/g, '-')
.replace(/\)/g, '')
.replace(/^\[/g, '(#')
.replace(/\]$/g, ')');
tmp += line + slug + '\n';
});
fs.writeFileSync('./01_aaatoc.md', tmp);
});
"
cat << 'EOF' > "${OUT}"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
EOF
echo "<title>${TITLE}</title>" >> "${OUT}"
cat << 'EOF' >> "${OUT}"
echo "<title>${TITLE}</title>" >> "${OUT}"
cat >>"${OUT}" << 'EOF'
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,300,600" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:400" rel="stylesheet" type="text/css">
<link href="https://s3-eu-west-1.amazonaws.com/jack12816/public/pastes/markdown.css" rel="stylesheet"></link>
<link rel="stylesheet" href="https://yandex.st/highlightjs/7.3/styles/github.min.css">
<script src="https://yandex.st/highlightjs/7.3/highlight.min.js"></script>
<script src="https://codeorigin.jquery.com/jquery-2.0.3.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/github.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/accesslog.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
hljs.tabReplace = ' ';
hljs.initHighlightingOnLoad();
$( function () {
// Add horizontal lines before h1 headings
$('h1:not(:nth-child(2))').prepend('<hr>');
// Break \n to <br> on highlightes examples (the result is faulty code
// but it is readable for a human)
setTimeout(function() {
$('.string').each(function(idx, itm) {
itm = $(itm);
itm.html(itm.html().replace(/\\n/gi, '<br>'));
})
}, 500);
} );
hljs.tabReplace = ' ';
hljs.initHighlightingOnLoad();
$(function() {
// Add horizontal lines before h1 headings
$('h1').slice(1).prepend('<hr>');
// Break \n to <br> on highlightes examples (the result is faulty code
// but it is readable for a human)
setTimeout(function() {
$('.string').each(function(idx, itm) {
itm = $(itm);
itm.html(itm.html().replace(/\\n/gi, '<br>'));
})
}, 500);
// Open images in a new tab
$('img').click(function() {
window.open($(this).attr('src'));
})
});
</script>
</head>
<body>
<div class="hidden-on-print" style="position: fixed; right: 20px; bottom: 20px;">
<a class="btn btn-primary" href="#Table of contents">Table of contents</a>
<a class="btn btn-primary" href="#table-of-contents">Table of contents</a>
</div>
EOF
for file in $(find *.md); do
echo -e "\n" >> "${TMP}"
cat "${file}" >> "${TMP}"
echo -e "\n" >> "${TMP}"
done
# Build anchors for all headlines
cat "${TMP}" | grep '^#' | node -e "
var fs = require('fs');
var chunks = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
chunks += chunk;
});
process.stdin.on('end', function() {
var tmp = fs.readFileSync('./__index.tmp', 'utf8');
chunks.split('\n').forEach(function(line) {
if ('' == line) {
return;
}
var slug = line.split(' ');
slug.shift();
slug = slug.join('-');
slug = slug.replace(/\(/g, '')
.replace(/\./g, '-')
.replace(/\,/g, '-')
.replace(/\)/g, '');
var heading = line.replace(/\(/g, '\\\(')
.replace(/\)/g, '\\\)')
.replace(/\[/g, '\\\[')
.replace(/\]/g, '\\\]')
.replace(/\"/g, '\\\"')
.replace(/\./g, '\\\.');
tmp = tmp.replace(
new RegExp('^' + heading + '\$', 'mgi'),
line + '<a name=\"' + slug + '\"></a>'
);
});
fs.writeFileSync('./__index.tmp', tmp);
});
"
marked --gfm --tables --lang-prefix "" "${TMP}" >> "${OUT}";
cat << 'EOF' >> "${OUT}"
</body>
</html>
EOF
rm "${TMP}" "${TOC}" 2>/dev/null
marked --gfm --tables --lang-prefix "" "${FCNT}" >> "${OUT}";
echo '</body></html>' >> "${OUT}"
rm -rf "${TOC}" "${CNT}" "${FCNT}" 2>/dev/null
}
function dirs-size()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment