Skip to content

Commit b436c98

Browse files
committed
Handling .Rmd files.
1 parent c76118e commit b436c98

File tree

7 files changed

+59
-8
lines changed

7 files changed

+59
-8
lines changed

.config.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
collections:
55
en:
66
output: true
7-
permalink: /:collection/:title/
7+
permalink: /:collection/:title.html
88

99
# Specify the use of the "lesson" template for elements of the "en" collection.
1010
defaults:
@@ -42,5 +42,7 @@ exclude:
4242
- misc
4343
- node_modules
4444
- requirements.txt
45+
- site.mk
46+
- site.yml
4547
- src
4648
- tex

Makefile

+18-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ endif
77
# Pick up project-specific setting for STEM.
88
include site.mk
99

10+
# Overall configuration file.
11+
CONFIG_YML=_config.yml
12+
1013
# Tools.
1114
JEKYLL=jekyll
1215
PANDOC=pandoc
@@ -15,13 +18,15 @@ BIBTEX=bibtex
1518
PYTHON=python
1619

1720
# Language-dependent settings.
18-
CONFIG_YML=_config.yml
1921
DIR_MD=_${lang}
22+
DIR_RMD=${lang}_rmd
23+
RMD_SRC=$(wildcard ${DIR_RMD}/*.Rmd)
24+
RMD_DST=$(patsubst ${DIR_RMD}/%.Rmd,${DIR_MD}/%.md,${RMD_SRC})
2025
PAGES_MD=$(wildcard ${DIR_MD}/*.md)
2126
BIB_MD=${DIR_MD}/bib.md
2227
TOC_JSON=_data/${lang}_toc.json
2328
DIR_HTML=_site/${lang}
24-
PAGES_HTML=${DIR_HTML}/index.html $(patsubst ${DIR_MD}/%.md,${DIR_HTML}/%/index.html,$(filter-out ${DIR_MD}/index.md,${PAGES_MD}))
29+
PAGES_HTML=$(patsubst ${DIR_MD}/%.md,${DIR_HTML}/%.html,${PAGES_MD})
2530
DIR_TEX=tex/${lang}
2631
BIB_TEX=${DIR_TEX}/book.bib
2732
ALL_TEX=${DIR_TEX}/all.tex
@@ -54,6 +59,9 @@ config : ${CONFIG_YML}
5459
## toc : regenerate the table of contents JSON file.
5560
toc : ${TOC_JSON}
5661

62+
## rmarkdown : rebuild Markdown source from R Markdown.
63+
rmarkdown : ${RMD_DST}
64+
5765
# ----------------------------------------
5866

5967
# Regenerate PDF once 'all.tex' has been created.
@@ -83,6 +91,10 @@ test-pandoc:
8391
| ${PYTHON} bin/transform.py --pre ${lang} _includes \
8492
| ${PANDOC} --wrap=preserve -f html -t latex -o -
8593

94+
# Build Markdown from R Markdown.
95+
${DIR_MD}/%.md : ${DIR_RMD}/%.Rmd
96+
@bin/build.R $< $@
97+
8698
# Create all the HTML pages once the Markdown files are up to date.
8799
${PAGES_HTML} : ${PAGES_MD} ${BIB_MD} ${CONFIG_YML} ${TOC_JSON}
88100
${JEKYLL} build
@@ -188,14 +200,17 @@ clean :
188200
@find . -name '*~' -delete
189201
@find . -name __pycache__ -prune -exec rm -r "{}" \;
190202
@find . -name '_minted-*' -prune -exec rm -r "{}" \;
191-
@rm -r -f tex/*/all.tex tex/*/*.aux tex/*/*.bbl tex/*/*.blg tex/*/*.log tex/*/*.out tex/*/*.toc
203+
@rm -f tex/*/all.tex tex/*/*.{aux,bbl,blg,lof,log,lot,out,toc}
192204
@find . -name .DS_Store -prune -exec rm -r "{}" \;
205+
@touch ${RMD_SRC} # to ensure rebuild the next time
193206

194207
## settings : show macro values.
195208
settings :
196209
@echo "CONFIG_YML=${CONFIG_YML}"
197210
@echo "JEKYLL=${JEKYLL}"
198211
@echo "DIR_MD=${DIR_MD}"
212+
@echo "RMD_SRC=${RMD_SRC}"
213+
@echo "RMD_DST=${RMD_DST}"
199214
@echo "PAGES_MD=${PAGES_MD}"
200215
@echo "BIB_MD=${BIB_MD}"
201216
@echo "DIR_HTML=${DIR_HTML}"

_includes/toc-bib.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
<p>
66
{% if page.slug == "bib" %}<strong>{% endif %}
7-
<a href="{{'/'|append: include.language|append: '/bib/'|relative_url}}" class="toc">Bibliography</a>
7+
<a href="{{'/'|append: include.language|append: '/bib.html'|relative_url}}" class="toc">Bibliography</a>
88
{% if page.slug == "bib" %}</strong>{% endif %}
99
</p>

_includes/toc-section.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
{% comment %}If there's a match, add a link to the generated ToC.{% endcomment %}
1717
{%- if lesson == chap.slug -%}
1818
<li>{% if page.slug == chap.slug %}<strong>{% endif %}
19-
<a href="{{'/'|append: include.language|append: '/'|append: chap.slug|append: '/'|relative_url}}" class="toc">
19+
<a href="{{'/'|append: include.language|append: '/'|append: chap.slug|append: '.html'|relative_url}}" class="toc">
2020
{% if chap.undone %}<em>{% endif %}{{chap.title}}{% if chap.undone %} &#9734;</em>{% endif %}
2121
</a>
2222
{% if page.slug == chap.slug %}</strong>{% endif %}

bin/build.R

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env Rscript
2+
3+
library(stringr)
4+
library(purrr)
5+
library(knitr)
6+
7+
USAGE = "Knit an RMarkdown files to create a Markdown file.
8+
9+
Usage: build.R source dest"
10+
11+
args <- commandArgs(trailingOnly = TRUE)
12+
if (((length(args) == 1) && (args[1] %in% c("-h", "--help"))) || (length(args) != 2)) {
13+
message(USAGE)
14+
quit(status = 0)
15+
}
16+
knit(args[1], output = args[2])

bin/get_body.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_all(source_dir):
3535
def make_filenames(source_dir, slugs):
3636
'''Turn slugs into filenames.'''
3737

38-
return [os.path.join(source_dir, s, 'index.html') for s in slugs]
38+
return [os.path.join(source_dir, '{}.html'.format(s)) for s in slugs]
3939

4040

4141
#-------------------------------------------------------------------------------

bin/transform.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import re
1010
from util import CHARACTERS, usage, get_crossref, get_toc
1111

12-
PATH_TO_ROOT = '../../' # from TeX directory to root of project
12+
PATH_TO_ROOT = '../..' # from TeX directory to root of project
13+
PATH_FOR_RMD = '../' # extra prefix for files generated from .Rmd
1314

1415
#-------------------------------------------------------------------------------
1516

@@ -313,6 +314,22 @@ class Figure(BaseRegexp):
313314
\end{{figure}}'''.replace('PATH_TO_ROOT', PATH_TO_ROOT)
314315

315316

317+
class FigureRmd(BaseRegexp):
318+
'''
319+
<p><img src="../figures/filename" alt="some text" /></p> # generated by knitr
320+
=>
321+
LaTeX: \begin{figure}\label{f:LABEL}\centering\includegraphics{PATH}\caption{TEXT}\end{figure}
322+
'''
323+
MATCH_HTML = r'<p><img\s+src="([^"]+)"\s+alt="(.+?)"\s+/></p>'
324+
WRITE_TEMP = r'==figurermd=={0}=={1}=='
325+
MATCH_TEMP = r'==figurermd==(.+)==(.+)=='
326+
WRITE_LATEX = r'''\begin{{figure}}
327+
\centering
328+
\includegraphics{{PATH_FOR_RMD/{0}}}
329+
\caption{{{1}}}
330+
\end{{figure}}'''.replace('PATH_FOR_RMD', PATH_FOR_RMD)
331+
332+
316333
class FigureRef(BaseRegexp):
317334
'''
318335
References to figures.
@@ -438,6 +455,7 @@ class Newline(BaseStringMatch):
438455
GlossaryEntry,
439456
CrossRef,
440457
Figure,
458+
FigureRmd,
441459
FigureRef,
442460
Table,
443461
TableRef,

0 commit comments

Comments
 (0)