Skip to content

WIP: Switch to markdown and fix code blocks #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
WIP: Switch to markdown and fix code blocks
TODO: 22/23 pages.
TODO: maybe chose another syntax color (See README.md)

I had a lot of difficulties while reading this great book, and most
where related to code blocks being badly formatted. For instance, the
`RCLASS(tmp)->iv_tbl` would render as `RCLASS->iv_tbl`.

Rather than fixing textile which really is not the goto markup language
nowadays. I've made another attempt to switch to markdown (seeing that
the former wasn't successful).

I've done that using the script below, and allowing myself a few
changes:

1. add syntax highlighter (See README.md),
2. wrap images in `figure` html tags, and show captions below,
3. make a few small cosmetic changes (See styles.css),
4. remove the old textile related plugin,
5. remove differentiation between small and large code blocks.

```awk
BEGIN {
	# Set it to 1 and remove the first `!` of any rule to only print that rule.
	DEBUG = 0
	figure_count = 0
	in_code = 0
}

function esc(s) {
	gsub("%", "%%", s)
	return s
}

function print_all_but_first() {
	for (i=2; i<NF; i++) printf esc($i) " "
	printf esc($NF) "\n"
}
function print_header(char) {
	print_all_but_first()
	n = length - length($1) - 1
	while(n--) printf(char)
	printf("\n")
}

!DEBUG && !in_code && /^#/ {
	printf "* "
	print_all_but_first()
	next
}
!DEBUG && !in_code && /^h1\./ { print_header("-"); next }
!DEBUG && !in_code && /^h2\./ { print_header("="); next }
!DEBUG && !in_code && /^h([3-9])\./ {
	n = substr($1, 2, 1)
	while(n--) printf("#")
	printf " "
	print_all_but_first()
	next
}
!DEBUG && !in_code && /^<pre/ {
	in_code = 1
	print "```TODO-lang"
	next
}
!DEBUG && in_code && /^<\/pre/ {
	in_code = 0
	print "```"
	next
}
!DEBUG && !in_code && /^!images\// {
	figure = ++figure_count
	match($0, /images\/[^(]+/)
	path = substr($0, RSTART, RLENGTH)
	match($0, /\([^)]+/)
	alt = substr($0, RSTART + 1, RLENGTH - 1)
	alt = "figure "figure": "alt
	print "<figure>"
	print "\t<img src=\""path"\" alt=\""alt"\">"
	print "\t<figcaption>"alt"</figcaption>"
	print "</figure>"
	next
}
!DEBUG && !in_code && /"[^""]+":[^ ]+/ {
	rv = $0
	i = 10
	while(match(rv, /"[^""]+":[^ ]+/)) {
		if (!i--) exit 1 # inifinte loop somehow in preface.textile, change by hand.

		full_length = RLENGTH
		match(rv, /"[^""]+":[^ ]/)
		text = substr(rv, RSTART + 1, RLENGTH - 4)
		link = substr(rv, RSTART + RLENGTH - 1, full_length - RLENGTH + 1)
		if (match(link, /^[a-z]+\.html$/)) link = substr(link, 0, length(link) - length(".html"))
		sub(/"[^""]+":[^ ]+/, "["text"]("link")TODO-checklink", rv)
	}
	print rv
	next
}
!DEBUG { print } # default (thanks to `next` in every action)
```

Signed-off-by: Ulysse Buonomo <buonomo.ulysse@gmail.com>
  • Loading branch information
BuonOmo committed Apr 17, 2021
commit c750418c3f75f3c70c2788f3b37ca4a052ce7c95
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
_site
/_site
/.jekyll-cache
ebooks
*#
*~
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source "https://rubygems.org"
gem "jekyll"
gem "RedCloth"
gem "rouge"
90 changes: 59 additions & 31 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,40 +1,68 @@
GEM
remote: https://rubygems.org/
specs:
RedCloth (4.2.9)
classifier (1.3.3)
fast-stemmer (>= 1.0.0)
colorator (0.1)
commander (4.1.3)
highline (~> 1.6.11)
directory_watcher (1.4.1)
fast-stemmer (1.0.2)
highline (1.6.19)
jekyll (1.0.3)
classifier (~> 1.3)
colorator (~> 0.1)
commander (~> 4.1.3)
directory_watcher (~> 1.4.1)
kramdown (~> 1.0.2)
liquid (~> 2.3)
maruku (~> 0.5)
pygments.rb (~> 0.5.0)
safe_yaml (~> 0.7.0)
kramdown (1.0.2)
liquid (2.5.0)
maruku (0.6.1)
syntax (>= 1.0.0)
posix-spawn (0.3.6)
pygments.rb (0.5.2)
posix-spawn (~> 0.3.6)
yajl-ruby (~> 1.1.0)
safe_yaml (0.7.1)
syntax (1.0.0)
yajl-ruby (1.1.0)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
colorator (1.1.0)
concurrent-ruby (1.1.8)
em-websocket (0.5.2)
eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0)
eventmachine (1.2.7)
ffi (1.15.0)
forwardable-extended (2.6.0)
http_parser.rb (0.6.0)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
jekyll (4.2.0)
addressable (~> 2.4)
colorator (~> 1.0)
em-websocket (~> 0.5)
i18n (~> 1.0)
jekyll-sass-converter (~> 2.0)
jekyll-watch (~> 2.0)
kramdown (~> 2.3)
kramdown-parser-gfm (~> 1.0)
liquid (~> 4.0)
mercenary (~> 0.4.0)
pathutil (~> 0.9)
rouge (~> 3.0)
safe_yaml (~> 1.0)
terminal-table (~> 2.0)
jekyll-sass-converter (2.1.0)
sassc (> 2.0.1, < 3.0)
jekyll-watch (2.2.1)
listen (~> 3.0)
kramdown (2.3.1)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
liquid (4.0.3)
listen (3.5.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
mercenary (0.4.0)
pathutil (0.16.2)
forwardable-extended (~> 2.6)
public_suffix (4.0.6)
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
rexml (3.2.5)
rouge (3.26.0)
safe_yaml (1.0.5)
sassc (2.4.0)
ffi (~> 1.9)
terminal-table (2.0.0)
unicode-display_width (~> 1.1, >= 1.1.1)
unicode-display_width (1.7.0)

PLATFORMS
ruby

DEPENDENCIES
RedCloth
jekyll
rouge

BUNDLED WITH
2.2.11
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -37,6 +37,24 @@ $ jekyll serve # this compiles files and starts a server on localhost:4000.
[Jekyll usage](https://github.com/mojombo/jekyll/wiki/usage)


<details>

<summary>Changing code style</summary>

```zsh
function apply_style {
pygmentize -S $1 -f html -a .highlight > css/highlight.css
}
while read -r style; do
echo $style
apply_style $style
read -qs && echo $style >> t.preselection
done < <(pygmentize -L styles | awk -F'[ :]' '/^\* / { print $2 }')
```

</details>


Reading in EPUB
=========

10 changes: 8 additions & 2 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
redcloth:
hard_breaks: false
exclude: ["ebooks", "script", "original_repo", "Gemfile", "Gemfile.lock", "dictionary.txt", "README.md"]
markdown: kramdown
highlighter: rouge
pygments: true
kramdown:
syntax_highlighter: rouge
input: GFM
auto_ids: true
toc_levels: 1..3
1 change: 1 addition & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
<meta charset="utf-8"/>
<title> {% if page.title %} {{ page.title }} | {% endif %} Ruby Hacking Guide </title>
<link rel="stylesheet" href="/css/styles.css" />
<link rel="stylesheet" href="/css/highlight.css" />
</head>
<body>

59 changes: 0 additions & 59 deletions _plugins/rhg_textile_converter.rb

This file was deleted.

82 changes: 82 additions & 0 deletions css/highlight.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #f8f8f8; }
.highlight .c { color: #8f5902; font-style: italic } /* Comment */
.highlight .err { color: #a40000; border: 1px solid #ef2929 } /* Error */
.highlight .g { color: #000000 } /* Generic */
.highlight .k { color: #204a87; font-weight: bold } /* Keyword */
.highlight .l { color: #000000 } /* Literal */
.highlight .n { color: #000000 } /* Name */
.highlight .o { color: #ce5c00; font-weight: bold } /* Operator */
.highlight .x { color: #000000 } /* Other */
.highlight .p { color: #000000; font-weight: bold } /* Punctuation */
.highlight .ch { color: #8f5902; font-style: italic } /* Comment.Hashbang */
.highlight .cm { color: #8f5902; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #8f5902; font-style: italic } /* Comment.Preproc */
.highlight .cpf { color: #8f5902; font-style: italic } /* Comment.PreprocFile */
.highlight .c1 { color: #8f5902; font-style: italic } /* Comment.Single */
.highlight .cs { color: #8f5902; font-style: italic } /* Comment.Special */
.highlight .gd { color: #a40000 } /* Generic.Deleted */
.highlight .ge { color: #000000; font-style: italic } /* Generic.Emph */
.highlight .gr { color: #ef2929 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00A000 } /* Generic.Inserted */
.highlight .go { color: #000000; font-style: italic } /* Generic.Output */
.highlight .gp { color: #8f5902 } /* Generic.Prompt */
.highlight .gs { color: #000000; font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #a40000; font-weight: bold } /* Generic.Traceback */
.highlight .kc { color: #204a87; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #204a87; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #204a87; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #204a87; font-weight: bold } /* Keyword.Pseudo */
.highlight .kr { color: #204a87; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #204a87; font-weight: bold } /* Keyword.Type */
.highlight .ld { color: #000000 } /* Literal.Date */
.highlight .m { color: #0000cf; font-weight: bold } /* Literal.Number */
.highlight .s { color: #4e9a06 } /* Literal.String */
.highlight .na { color: #c4a000 } /* Name.Attribute */
.highlight .nb { color: #204a87 } /* Name.Builtin */
.highlight .nc { color: #000000 } /* Name.Class */
.highlight .no { color: #000000 } /* Name.Constant */
.highlight .nd { color: #5c35cc; font-weight: bold } /* Name.Decorator */
.highlight .ni { color: #ce5c00 } /* Name.Entity */
.highlight .ne { color: #cc0000; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #000000 } /* Name.Function */
.highlight .nl { color: #f57900 } /* Name.Label */
.highlight .nn { color: #000000 } /* Name.Namespace */
.highlight .nx { color: #000000 } /* Name.Other */
.highlight .py { color: #000000 } /* Name.Property */
.highlight .nt { color: #204a87; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #000000 } /* Name.Variable */
.highlight .ow { color: #204a87; font-weight: bold } /* Operator.Word */
.highlight .w { color: #f8f8f8; text-decoration: underline } /* Text.Whitespace */
.highlight .mb { color: #0000cf; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000cf; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000cf; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000cf; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000cf; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #4e9a06 } /* Literal.String.Affix */
.highlight .sb { color: #4e9a06 } /* Literal.String.Backtick */
.highlight .sc { color: #4e9a06 } /* Literal.String.Char */
.highlight .dl { color: #4e9a06 } /* Literal.String.Delimiter */
.highlight .sd { color: #8f5902; font-style: italic } /* Literal.String.Doc */
.highlight .s2 { color: #4e9a06 } /* Literal.String.Double */
.highlight .se { color: #4e9a06 } /* Literal.String.Escape */
.highlight .sh { color: #4e9a06 } /* Literal.String.Heredoc */
.highlight .si { color: #4e9a06 } /* Literal.String.Interpol */
.highlight .sx { color: #4e9a06 } /* Literal.String.Other */
.highlight .sr { color: #4e9a06 } /* Literal.String.Regex */
.highlight .s1 { color: #4e9a06 } /* Literal.String.Single */
.highlight .ss { color: #4e9a06 } /* Literal.String.Symbol */
.highlight .bp { color: #3465a4 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #000000 } /* Name.Function.Magic */
.highlight .vc { color: #000000 } /* Name.Variable.Class */
.highlight .vg { color: #000000 } /* Name.Variable.Global */
.highlight .vi { color: #000000 } /* Name.Variable.Instance */
.highlight .vm { color: #000000 } /* Name.Variable.Magic */
.highlight .il { color: #0000cf; font-weight: bold } /* Literal.Number.Integer.Long */
25 changes: 20 additions & 5 deletions css/styles.css
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ body {
line-height: 140%;
margin-top: 5%;
margin-left: 2em;
width: 40em;
width: 35em;
}

h1 {
@@ -72,13 +72,16 @@ dd {
}

p.caption {
margin-botton: 0px;
margin-bottom: 0px;
}

pre {
line-height: 120%;
.highlight {
padding: 8px;
background: #eee;
border-radius: 8px;
}

.highlight pre {
margin: 0;
}

span.ami {
@@ -121,6 +124,18 @@ body {
font-family: Georgia, serif;
}

figcaption {
font-size: 0.8em;
font-style: italic;
}

/* code inlined in text */
code.highlighter-rouge {
background: #33a2;
border-radius: 2px;
padding: 2px;
}

h1 code, h2 code, h3 code, h4 code, h5 code{
font-family: Menlo, Monaco, "Andale Mono", "Lucida console", "courier New", monospace;
font-weight: normal;
149 changes: 81 additions & 68 deletions variable.textile → variable.md

Large diffs are not rendered by default.