1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Markdownを印刷する

Last updated at Posted at 2020-02-16

html化にはqiita-markdownを使用。activesupportを前もって入れているのは、qiita-markdownのdependencyにactivesupportを要求するのがあり、最新のactivesupportはRuby 2.3にはインストールできないからである。
なおこのactivesupportはqiita-markdownのdevelopment dependencyと同じバージョン。

pdf化には、wkhtmltopdfを使いたかったが、先頭のfont-familyだけが使われるバグがあるようなので、google-chromeを使うしかなさそう。ヘッダを消去するためにmarginを調整。

if ! ruby -rqiita-markdown -e1 2>/dev/null; then
  sudo gem install -v 4.2.6 activesupport --no-ri
  # sudo gem install charlock_holmes --with-cxxflags=\'-Wno-reserved-user-defined-literal -std=c++11\'
  sudo gem install -v 0.27.0 qiita-markdown --no-ri
fi

dir=$(dirname "$(basename "$0")")
cd "$dir"
./md2html.rb src.md
google-chrome --headless  --disable-gpu --print-to-pdf=src.md.pdf src.md.html

CSSはdefault-theme.cssが使える模様。
https://github.com/increments/kobito-oss/blob/master/application/src/styles/default-theme.scss をコンパイルすることで取得できます。最短手順は以下。

brew install sass/sass/sass
script/build
cd application/src/styles
sass default-theme.scss > default-theme.css

本来ハイライトエンジンがhighlight.jsとpygments.rbで違うので、もろもろおかしくてもしかたないのだが、幸いmarkdown_content.scss部分がまあまあいい感じに効くらしい。

一応。https://pastebin.com/4fTAxbHa にもgz.b64を置いておきました。

md2html.rb
# !/usr/bin/ruby
# coding:utf-8

require 'qiita-markdown'

dir = File.dirname File.realpath __FILE__
if ARGV.empty?
  STDERR.puts 'generate.rb source.md'
  exit
end
mdfile = ARGV[0]

doc = File.read mdfile
processor = Qiita::Markdown::Processor.new hostname:'example.com'
parse_result = processor.(doc)
File.write mdfile+'.html',<<EOM
<html><head>
<link rel="stylesheet" href="default-theme.css" type="text/css"></link>
<style type="text/css">
<!--
@media print {
  @page {
    margin-top: 20px;
    margin-bottom: 20px;
  }
}
body {
  overflow: visible;
  word-break: break-all;
}
.highlight pre {
  width: 99%;
  white-space: pre-wrap;
}
.markdown-content .code-frame {
  background-color: #e8e8e8;
}
-->
</style>
</head><body>
<div class="markdown-content">
# {parse_result[:output].to_html}
</div>
</body></html>
EOM

追伸

OSX版KobitoのCSSが使いたくて実験環境作ろうとしたが、そもそもハイライトエンジンが違うから無駄だったっていうね。悲惨。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?