Skip to content

Commit 6bc1515

Browse files
committed
Adds build target
1 parent 3cfb80d commit 6bc1515

File tree

2 files changed

+110
-9
lines changed

2 files changed

+110
-9
lines changed

Makefile

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
FILTER_FILE := $(wildcard *.lua)
33
# Name of the filter, *without* `.lua` file extension
44
FILTER_NAME = $(patsubst %.lua,%,$(FILTER_FILE))
5+
# Source files
6+
# Optional: build the filter file from multiple sources.
7+
# *Do not comment out!* To deactivate it's safer to
8+
# define an empty SOURCE_MAIN variable with:
9+
# SOURCE_MAIN =
10+
SOURCE_DIR =
11+
SOURCE_MAIN =
12+
SOURCE_MODULES =
13+
#
14+
SOURCE_FILES = $(SOURCE_MAIN:%=$(SOURCE_DIR)%.lua) $(SOURCE_MODULES:%=$(SOURCE_DIR)%.lua)
515

616
# Allow to use a different pandoc binary, e.g. when testing.
717
PANDOC ?= pandoc
@@ -37,26 +47,70 @@ help:
3747
'/^## / h ; /^[^_.$$#][^ ]+:/ { G; s/^(.*):.*##(.*)/\1@\2/; P ; h }' \
3848
$(MAKEFILE_LIST) | tr @ '\t'
3949

50+
#
51+
# Build
52+
#
53+
54+
## Build the filter file from sources (requires luacc)
55+
# If SOURCE_MAIN is not empty, combine source files with
56+
# luacc and replace the filter file.
57+
# ifeq is safer than ifdef (easier for the user to make
58+
# the variable empty than to make it undefined).
59+
.PHONY: build
60+
build: _build
61+
62+
ifneq ($(SOURCE_MAIN), )
63+
.PHONY: _build
64+
_build: _check_luacc $(SOURCE_FILES)
65+
@if [ -f $(QUARTO_EXT_DIR)/$(FILTER_FILE) ]; then \
66+
luacc -o $(QUARTO_EXT_DIR)/$(FILTER_FILE) -i $(SOURCE_DIR) \
67+
$(SOURCE_DIR)/$(SOURCE_MAIN) $(SOURCE_MODULES); \
68+
if [ ! -L $(FILTER_FILE) ]; then \
69+
ln -s $(QUARTO_EXT_DIR)/$(FILTER_FILE) $(FILTER_FILE); \
70+
fi \
71+
else \
72+
luacc -o $(FILTER_FILE) -i $(SOURCE_DIR) \
73+
$(SOURCE_DIR)/$(SOURCE_MAIN) $(SOURCE_MODULES); \
74+
fi
75+
else
76+
.PHONY: _build
77+
_build:
78+
79+
endif
80+
81+
.PHONY: check_luacc
82+
_check_luacc:
83+
@if ! command -v luacc &> /dev/null ; then \
84+
echo "LuaCC is needed to build the filter. Available on LuaRocks:"; \
85+
echo " https://luarocks.org/modules/mihacooper/luacc"; \
86+
exit; \
87+
fi
88+
4089
#
4190
# Test
4291
#
4392

44-
## Test that running the filter on the sample input yields expected output
93+
## Test that running the filter on the sample input yields expected outputs
4594
# The automatic variable `$<` refers to the first dependency
4695
# (i.e., the filter file).
4796
# let `test` be a PHONY target so that it is run each time it's called.
4897
.PHONY: test
4998
test: $(FILTER_FILE) test/input.md test/test.yaml
50-
$(PANDOC) --defaults test/test.yaml | \
51-
$(DIFF) test/expected.native -
52-
99+
@for ext in $(FORMAT) ; do \
100+
$(PANDOC) --defaults test/test.yaml --to $$ext | \
101+
$(DIFF) test/expected.$$ext - ; \
102+
done
53103

54-
## Re-generate the expected output
55-
# This file **must not** be a dependency of the `test` target, as that
104+
## Generate the expected output
105+
# This target **must not** be a dependency of the `test` target, as that
56106
# would cause it to be regenerated on each run, making the test
57107
# pointless.
58-
test/expected.native: $(FILTER_FILE) test/input.md test/test.yaml
59-
$(PANDOC) --defaults test/test.yaml --output=$@
108+
.PHONY: generate
109+
generate: build $(FILTER_FILE) test/input.md test/test.yaml
110+
@for ext in $(FORMAT) ; do \
111+
$(PANDOC) --defaults test/test.yaml --to $$ext \
112+
--output test/expected.$$ext ; \
113+
done
60114

61115
#
62116
# Website
@@ -132,7 +186,7 @@ $(QUARTO_EXT_DIR)/$(FILTER_FILE): $(FILTER_FILE) $(QUARTO_EXT_DIR)
132186

133187
## Sets a new release (uses VERSION macro if defined)
134188
.PHONY: release
135-
release: quarto-extension
189+
release: build quarto-extension generate
136190
git commit -am "Release $(FILTER_NAME) $(VERSION)"
137191
git tag v$(VERSION) -m "$(FILTER_NAME) $(VERSION)"
138192
@echo 'Do not forget to push the tag back to github with `git push --tags`'

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,53 @@ contents of this README, an example generated from the test input,
132132
as well as the full filter code. The page components are combined
133133
with the `.tools/docs.lua` filter.
134134

135+
#### `build`
136+
137+
Build the filter from multiple source files. Use this target if you
138+
want to develop the filter as multiple source files but distribute
139+
it in a single source.
140+
141+
Requires [Lua Code Combiner][luacc], available on LuaRocks. Install
142+
with `luarocks install luacc` (requires [LuaRocks][luarocks]).
143+
144+
[luacc]: https://luarocks.org/modules/mihacooper/luacc
145+
[luarocks]: https://luarocks.org
146+
147+
__Setup__. Suppose your source files are as follows:
148+
149+
```
150+
/path/to/filter
151+
|-src/
152+
|-main.lua
153+
|-module1.lua
154+
|-module2.lua
155+
```
156+
157+
Where `main.lua` uses the module files with the standard Lua
158+
function `require('module1')` or `myvar = require('module2')`.
159+
160+
Edit the Makefile lines below as follows:
161+
162+
```
163+
SOURCE_DIR = src/
164+
SOURCE_MAIN = main
165+
SOURCE_MODULES = module1 module2
166+
```
167+
168+
__Usage__. `build` uses LuaCC to combine the main source and its
169+
module into a single Lua file which will **replace** your filter's
170+
file. It is automatically called when using `generate` and `release`.
171+
172+
173+
Deactivate by setting `SOURCE_MAIN` to be empty:
174+
175+
```
176+
SOURCE_MAIN =
177+
```
178+
179+
Commenting this line out is not recommended, as the variable
180+
may remain defined in your shell session.
181+
135182
### Website
136183

137184
The repository template comes with a GitHub Action to publish a

0 commit comments

Comments
 (0)