support publishing output to a `.docx` file, and support templated word docs, too, preloaded with configured styles
joshlong opened this issue · comments
it should be possible to get a word document out of the process. indeed, it should be possible to get a word document that in turn has some templated styles already in place. this way the resulting document looks like it should automatically. imagine a book publisher's pipeline, for example. so heres a Makefile
that Java legends Joseph Ottinger and Andrew Lombardi shared.
SOURCEDOC=$(wildcard ./*.adoc)
HTMLFILES=$(SOURCEDOC:%.adoc=output/%.html)
DOCXFILES=$(SOURCEDOC:%.adoc=output/%.adoc.docx)
publish: clean $(HTMLFILES)
mkdir -p output/images
-cp images/* output/images
doc: publish $(DOCXFILES)
install:
brew install graphviz plantuml
# presumes localized ruby
gem install asciidoctor-diagram
init:
.PHONY: clean publish doc .install
clean:
rm -rf output
output/%.html: %.adoc
asciidoctor -r asciidoctor-diagram -d book -a toc $< -D output
# TODO fix to use the admonitions lua script and the reference doc
output/%.adoc.docx: %.adoc
asciidoctor -r asciidoctor-diagram -d book --backend docbook --out-file - $< | \
pandoc -r docbook -t docx -o output/$<.docx --reference-doc custom-reference.docx --lua-filter lua/admonitions.lua
and this in turn uses a lua script which is
-- Edit this with labels and attributes for admonition div classes
local cls_data = {
note = {
label = 'Note',
label_attrs = {
['custom-style'] = 'NoteLabel'
},
text_attrs = {
['custom-style'] = 'NoteText'
}
},
warning = {
label = 'Warning',
label_attrs = {
['custom-style'] = 'NoteLabel'
},
text_attrs = {
['custom-style'] = 'NoteText'
}
}
}
-- Get the pandoc library under a shorter name
local p = assert(pandoc, "Cannot find the pandoc library")
if not ('table' == type(p)) then
error("Expected variable pandoc to be table")
end
-- Create the label divs
for cls, data in pairs(cls_data) do
data.label = p.Div({
p.Para({ p.Str(data.label) })
}, data.label_attrs)
end
-- The filter function
function Div (div)
for _, cls in ipairs(div.classes) do
local data = cls_data[cls] -- get data if any
if data then -- if this is an admonition class
-- Set the attributes on the div
for name, val in pairs(data.text_attrs) do
div.attributes[name] = val
end
-- Return the data and the div
return { data.label:clone(), div }
end
end
-- If no class matches
return nil
end
Perfect! Note that when we use it, we had to install asciidoctor as a gem, because of the diagram stuff. The "make install" bit was shorthand for the idiot authors, not meant for wider consumption!