Skip to content

Commit c3214b3

Browse files
committed
Merge branch 'lua-code-combine' into up-to-date
2 parents ba097bd + 6bc1515 commit c3214b3

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

Makefile

Lines changed: 51 additions & 2 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
@@ -42,6 +52,45 @@ help:
4252
'/^## / h ; /^[^_.$$#][^ ]+:/ { G; s/^(.*):.*##(.*)/\1@\2/; P ; h ; }' \
4353
$(MAKEFILE_LIST) | tr @ '\t'
4454

55+
#
56+
# Build
57+
#
58+
59+
## Build the filter file from sources (requires luacc)
60+
# If SOURCE_MAIN is not empty, combine source files with
61+
# luacc and replace the filter file.
62+
# ifeq is safer than ifdef (easier for the user to make
63+
# the variable empty than to make it undefined).
64+
.PHONY: build
65+
build: _build
66+
67+
ifneq ($(SOURCE_MAIN), )
68+
.PHONY: _build
69+
_build: _check_luacc $(SOURCE_FILES)
70+
@if [ -f $(QUARTO_EXT_DIR)/$(FILTER_FILE) ]; then \
71+
luacc -o $(QUARTO_EXT_DIR)/$(FILTER_FILE) -i $(SOURCE_DIR) \
72+
$(SOURCE_DIR)/$(SOURCE_MAIN) $(SOURCE_MODULES); \
73+
if [ ! -L $(FILTER_FILE) ]; then \
74+
ln -s $(QUARTO_EXT_DIR)/$(FILTER_FILE) $(FILTER_FILE); \
75+
fi \
76+
else \
77+
luacc -o $(FILTER_FILE) -i $(SOURCE_DIR) \
78+
$(SOURCE_DIR)/$(SOURCE_MAIN) $(SOURCE_MODULES); \
79+
fi
80+
else
81+
.PHONY: _build
82+
_build:
83+
84+
endif
85+
86+
.PHONY: check_luacc
87+
_check_luacc:
88+
@if ! command -v luacc &> /dev/null ; then \
89+
echo "LuaCC is needed to build the filter. Available on LuaRocks:"; \
90+
echo " https://luarocks.org/modules/mihacooper/luacc"; \
91+
exit; \
92+
fi
93+
4594
#
4695
# Test
4796
#
@@ -62,7 +111,7 @@ test: $(FILTER_FILE) test/input.md test/test.yaml
62111
# would cause it to be regenerated on each run, making the test
63112
# pointless.
64113
.PHONY: generate
65-
generate: $(FILTER_FILE) test/input.md test/test.yaml
114+
generate: build $(FILTER_FILE) test/input.md test/test.yaml
66115
@for ext in $(FORMAT) ; do \
67116
$(PANDOC) --defaults test/test.yaml --to $$ext \
68117
--output test/expected.$$ext ; \
@@ -142,7 +191,7 @@ $(QUARTO_EXT_DIR)/$(FILTER_FILE): $(FILTER_FILE) $(QUARTO_EXT_DIR)
142191

143192
## Sets a new release (uses VERSION macro if defined)
144193
.PHONY: release
145-
release: quarto-extension regenerate
194+
release: build quarto-extension generate
146195
git commit -am "Release $(FILTER_NAME) $(VERSION)"
147196
git tag v$(VERSION) -m "$(FILTER_NAME) $(VERSION)"
148197
@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
@@ -152,6 +152,53 @@ contents of this README, an example generated from the test input,
152152
as well as the full filter code. The page components are combined
153153
with the `.tools/docs.lua` filter.
154154

155+
#### `build`
156+
157+
Build the filter from multiple source files. Use this target if you
158+
want to develop the filter as multiple source files but distribute
159+
it in a single source.
160+
161+
Requires [Lua Code Combiner][luacc], available on LuaRocks. Install
162+
with `luarocks install luacc` (requires [LuaRocks][luarocks]).
163+
164+
[luacc]: https://luarocks.org/modules/mihacooper/luacc
165+
[luarocks]: https://luarocks.org
166+
167+
__Setup__. Suppose your source files are as follows:
168+
169+
```
170+
/path/to/filter
171+
|-src/
172+
|-main.lua
173+
|-module1.lua
174+
|-module2.lua
175+
```
176+
177+
Where `main.lua` uses the module files with the standard Lua
178+
function `require('module1')` or `myvar = require('module2')`.
179+
180+
Edit the Makefile lines below as follows:
181+
182+
```
183+
SOURCE_DIR = src/
184+
SOURCE_MAIN = main
185+
SOURCE_MODULES = module1 module2
186+
```
187+
188+
__Usage__. `build` uses LuaCC to combine the main source and its
189+
module into a single Lua file which will **replace** your filter's
190+
file. It is automatically called when using `generate` and `release`.
191+
192+
193+
Deactivate by setting `SOURCE_MAIN` to be empty:
194+
195+
```
196+
SOURCE_MAIN =
197+
```
198+
199+
Commenting this line out is not recommended, as the variable
200+
may remain defined in your shell session.
201+
155202
### Website
156203

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

0 commit comments

Comments
 (0)