diff --git a/Makefile b/Makefile
index 49f72b0..b84bd6e 100644
--- a/Makefile
+++ b/Makefile
@@ -9,14 +9,16 @@ MAKEFILE = Makefile
OUTPUT_FILENAME = book
METADATA = metadata.yml
CHAPTERS = chapters/*.md
-TOC = --toc --toc-depth=2
-METADATA_ARGS = --metadata-file=$(METADATA)
-IMAGES_FOLDER = images
-IMAGES = $(IMAGES_FOLDER)/*
-COVER_IMAGE = $(IMAGES_FOLDER)/cover.png
+TOC = --toc --toc-depth 2
+METADATA_ARGS = --metadata-file $(METADATA)
+IMAGES = $(shell find images -type f)
+TEMPLATES = $(shell find templates/ -type f)
+COVER_IMAGE = images/cover.png
MATH_FORMULAS = --webtex
-CSS_FILE = style.css
-CSS_ARGS = --css=$(CSS_FILE)
+
+# Chapters content
+CONTENT = awk 'FNR==1 && NR!=1 {print "\n\n"}{print}' $(CHAPTERS)
+CONTENT_FILTERS = tee # Use this to add sed filters or other piped commands
# Debugging
@@ -29,15 +31,24 @@ CSS_ARGS = --css=$(CSS_FILE)
# Combined arguments
-ARGS = $(TOC) $(MATH_FORMULAS) $(CSS_ARGS) $(METADATA_ARGS) $(FILTER_ARGS) $(DEBUG_ARGS)
+ARGS = $(TOC) $(MATH_FORMULAS) $(METADATA_ARGS) $(FILTER_ARGS) $(DEBUG_ARGS)
+
PANDOC_COMMAND = pandoc
# Per-format options
-EPUB_ARGS = --epub-cover-image=$(COVER_IMAGE)
-HTML_ARGS = --standalone --to=html5
-PDF_ARGS = -V geometry:margin=1in -V documentclass=report --pdf-engine=xelatex
-DOCX_ARGS =
+DOCX_ARGS = --standalone --reference-doc templates/docx.docx
+EPUB_ARGS = --template templates/epub.html --epub-cover-image $(COVER_IMAGE)
+HTML_ARGS = --template templates/html.html --standalone --to html5
+PDF_ARGS = --template templates/pdf.latex --pdf-engine xelatex
+
+# Per-format file dependencies
+
+BASE_DEPENDENCIES = $(MAKEFILE) $(CHAPTERS) $(METADATA) $(IMAGES) $(TEMPLATES)
+DOCX_DEPENDENCIES = $(BASE_DEPENDENCIES)
+EPUB_DEPENDENCIES = $(BASE_DEPENDENCIES)
+HTML_DEPENDENCIES = $(BASE_DEPENDENCIES)
+PDF_DEPENDENCIES = $(BASE_DEPENDENCIES)
####################################################################################################
# Basic actions
@@ -62,25 +73,23 @@ pdf: $(BUILD)/pdf/$(OUTPUT_FILENAME).pdf
docx: $(BUILD)/docx/$(OUTPUT_FILENAME).docx
-$(BUILD)/epub/$(OUTPUT_FILENAME).epub: $(MAKEFILE) $(METADATA) $(CHAPTERS) $(CSS_FILE) $(IMAGES) \
- $(COVER_IMAGE)
+$(BUILD)/epub/$(OUTPUT_FILENAME).epub: $(EPUB_DEPENDENCIES)
mkdir -p $(BUILD)/epub
- $(PANDOC_COMMAND) $(ARGS) $(EPUB_ARGS) -o $@ $(CHAPTERS)
+ $(CONTENT) | $(CONTENT_FILTERS) | $(PANDOC_COMMAND) $(ARGS) $(EPUB_ARGS) -o $@
@echo "$@ was built"
-$(BUILD)/html/$(OUTPUT_FILENAME).html: $(MAKEFILE) $(METADATA) $(CHAPTERS) $(CSS_FILE) $(IMAGES)
+$(BUILD)/html/$(OUTPUT_FILENAME).html: $(HTML_DEPENDENCIES)
mkdir -p $(BUILD)/html
- $(PANDOC_COMMAND) $(ARGS) $(HTML_ARGS) -o $@ $(CHAPTERS)
- cp -R $(IMAGES_FOLDER)/ $(BUILD)/html/$(IMAGES_FOLDER)/
- cp $(CSS_FILE) $(BUILD)/html/$(CSS_FILE)
+ $(CONTENT) | $(CONTENT_FILTERS) | $(PANDOC_COMMAND) $(ARGS) $(HTML_ARGS) -o $@
+ cp --parent $(IMAGES) $(BUILD)/html/
@echo "$@ was built"
-$(BUILD)/pdf/$(OUTPUT_FILENAME).pdf: $(MAKEFILE) $(METADATA) $(CHAPTERS) $(CSS_FILE) $(IMAGES)
+$(BUILD)/pdf/$(OUTPUT_FILENAME).pdf: $(PDF_DEPENDENCIES)
mkdir -p $(BUILD)/pdf
- $(PANDOC_COMMAND) $(ARGS) $(PDF_ARGS) -o $@ $(CHAPTERS)
+ $(CONTENT) | $(CONTENT_FILTERS) | $(PANDOC_COMMAND) $(ARGS) $(PDF_ARGS) -o $@
@echo "$@ was built"
-$(BUILD)/docx/$(OUTPUT_FILENAME).docx: $(MAKEFILE) $(METADATA) $(CHAPTERS) $(CSS_FILE) $(IMAGES)
+$(BUILD)/docx/$(OUTPUT_FILENAME).docx: $(DOCX_DEPENDENCIES)
mkdir -p $(BUILD)/docx
- $(PANDOC_COMMAND) $(ARGS) $(DOCX_ARGS) -o $@ $(CHAPTERS)
+ $(CONTENT) | $(CONTENT_FILTERS) | $(PANDOC_COMMAND) $(ARGS) $(DOCX_ARGS) -o $@
@echo "$@ was built"
diff --git a/README.md b/README.md
index 00c8243..0dae6cd 100644
--- a/README.md
+++ b/README.md
@@ -275,6 +275,25 @@ Table: This is an example table. {#tbl:table}
Check the desired filter settings and usage for more information
([pandoc-crossref usage](http://lierdakil.github.io/pandoc-crossref/)).
+#### Content filters
+
+If you need to modify the MD content before passing it to pandoc, you may use `CONTENT_FILTERS`. By
+setting this makefile variable, it will be passed to the markdown content before passing it to
+pandoc. For example, to replace all occurrences of `@pagebreak` with
+`
` you may use a `sed` filter:
+
+```
+CONTENT_FILTERS = sed 's/@pagebreak/"
<\/div>"/g'
+```
+
+To use multiple filters, you may include multiple pipes on the `CONTENT_FILTERS` variable:
+
+```
+CONTENT_FILTERS = \
+ sed 's/@pagebreak/"
<\/div>"/g' | \
+ sed 's/@image/[Cool image](\/images\/image.png)/g'
+```
+
### Output
This template uses *Makefile* to automatize the building process. Instead of using the *pandoc cli
@@ -332,6 +351,13 @@ If you want to configure the output, you'll probably have to look the
[Pandoc Manual](http://pandoc.org/MANUAL.html) for further information about pdf (LaTeX) generation,
custom styles, etc, and modify the Makefile file accordingly.
+#### Templates
+
+Output files are generated using [pandoc templates](https://pandoc.org/MANUAL.html#templates). All
+templates are located under the `templates/` folder, and may be modified as you will. Some basic
+format templates are already included on this repository, ion case you need something to start
+with.
+
## References
- [Pandoc](http://pandoc.org/)
diff --git a/style.css b/style.css
deleted file mode 100644
index a58cbb2..0000000
--- a/style.css
+++ /dev/null
@@ -1,3 +0,0 @@
-/*
- * Place your css content here.
- */
diff --git a/templates/docx.docx b/templates/docx.docx
new file mode 100644
index 0000000..d9ce0db
Binary files /dev/null and b/templates/docx.docx differ
diff --git a/templates/epub.html b/templates/epub.html
new file mode 100644
index 0000000..c32f645
--- /dev/null
+++ b/templates/epub.html
@@ -0,0 +1,69 @@
+
+
+
+
+
+
+ $pagetitle$
+$if(highlighting-css)$
+
+$endif$
+$for(css)$
+
+$endfor$
+$for(header-includes)$
+ $header-includes$
+$endfor$
+
+
+$if(titlepage)$
+
+$for(title)$
+$if(title.type)$
+