/
rtf
executable file
·33 lines (26 loc) · 1.03 KB
/
rtf
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
#!/usr/bin/ruby
# frozen_string_literal: true
require 'open3'
require 'pathname'
ENV['PATH'] = Open3.capture2(Pathname.pwd.join('_sharedresources').to_path, 'multimarkdown').first
Rich_text_style = '
<style>
body { font-family: sans-serif; font-size: 14; }
code { font-family: monospace; }
p { margin: 0; }
</style>
'
# An empty line is added to the beginning of STDIN so text starting with a word and a colon won't be considered metadata
# https://web.archive.org/web/20210804225002/https://fletcher.github.io/MultiMarkdown-5/syntax.html
Html = "#{Rich_text_style}#{Open3.capture2('multimarkdown', '--', stdin_data: "\n#{ARGV[0]}").first}".
gsub(/<img.*?>/, ''). # Strip images
gsub('</p>', '</p><br>') # Enforce line breaks after paragraphs
Rtf_text = Open3.capture2(
'textutil', '-inputencoding', 'utf8', '-convert', 'rtf',
'-stdin', '-stdout', '-format', 'html',
stdin_data: Html
).first.
sub(/\\\n\\\n}$/, '}')
rtf_text_lines = Rtf_text.split("\n")
rtf_text_lines.slice!(3..4)
print rtf_text_lines.join("\n")