Skip to content

Commit

Permalink
Use Makefile for compiling blog articles.
Browse files Browse the repository at this point in the history
  • Loading branch information
jumper149 committed Oct 11, 2022
1 parent 11f0232 commit dce92bf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 66 deletions.
2 changes: 1 addition & 1 deletion blog/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
out
build
46 changes: 46 additions & 0 deletions blog/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
SOURCE = source
BUILD = build
ARTICLES = $(wildcard $(SOURCE)/*.adoc)

ifndef AUTHOR
$(error AUTHOR is not set)
endif

ifndef BASEURL
$(error BASEURL is not set)
endif

ifndef EMAIL
$(error EMAIL is not set)
endif

ifndef HOMEPAGE
$(error HOMEPAGE is not set)
endif

ifndef REVNUMBER
$(error REVNUMBER is not set)
endif

ASCIIDOC_COMPILER = asciidoctor
ASCIIDOCTOR_FLAGS = $\
--doctype article $\
--safe-mode server $\
--attribute source-highlighter=rouge $\
--attribute prewrap! $\
--attribute author="$(AUTHOR)" $\
--attribute email=$(EMAIL) $\
--attribute homepage="$(HOMEPAGE)" $\
--attribute revnumber=$(REVNUMBER)

$(BUILD)/%.html: $(SOURCE)/%.adoc
mkdir -p $(BUILD)
asciidoctor "$(SOURCE)/$*.adoc" --out-file "$(BUILD)/$*.html" $(ASCIIDOCTOR_FLAGS) --attribute imagesdir="$(BASEURL)/blog/raw/$*" --backend html5 --attribute nofooter --attribute webfonts!
sed -i 's/^<head>$$/<head>\n<base target="_parent">/' "$(BUILD)/$*.html"

$(BUILD)/%.pdf: $(SOURCE)/%.adoc
mkdir -p $(BUILD)
asciidoctor-pdf "$(SOURCE)/$*.adoc" --out-file "$(BUILD)/$*.pdf" $(ASCIIDOCTOR_FLAGS) --attribute imagesdir="$*" --attribute pdf-theme="pdf-theme.yml"

clean:
rm -r $(BUILD)
2 changes: 1 addition & 1 deletion blog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ The articles are generated using Asciidoctor.
nix develop ..#subflakes.blog.devShells.x86_64-linux.default
# Generate a PDF preview of a new article `example.adoc`.
asciidoctor-pdf-blog example
make build/example.pdf
```
77 changes: 13 additions & 64 deletions blog/subflake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,22 @@
name = "blog"; # TODO: Necessary to avoid segmentation fault.
src = ./.;
buildPhase = ''
mkdir -p static
ASCIIDOCTOR_FLAG_LIST=(
"--doctype article"
"--safe-mode server"
"--attribute source-highlighter=rouge"
"--attribute prewrap!"
"--attribute email=${setup.config.contact-information.email-address}"
"--attribute revnumber="${if self ? rev then self.rev else "unknown-revision"}""
)
ASCIIDOCTOR_FLAGS="$(for flag in "''${ASCIIDOCTOR_FLAG_LIST[*]}"; do echo $flag; done)"
export AUTHOR="${setup.config.contact-information.name}"
export BASEURL="${import ./base-url.nix setup.config.base-url}"
export EMAIL="${setup.config.contact-information.email-address}"
export HOMEPAGE="${import ./base-url.nix setup.config.base-url}[${setup.config.contact-information.homepage-label}]"
export REVNUMBER="${if self ? rev then self.rev else "unknown-revision"}"
compileArticle() {
ARTICLE_NAME="$1"
if [ -f "source/$ARTICLE_NAME.adoc" ]
make "build/$ARTICLE_NAME.html"
make "build/$ARTICLE_NAME.pdf"
if [ -d "source/$ARTICLE_NAME" ]
then
echo "Article exists: '$ARTICLE_NAME'"
echo "HTML: '$ARTICLE_NAME'"
asciidoctor "source/$ARTICLE_NAME.adoc" --out-file "static/$ARTICLE_NAME.html" $ASCIIDOCTOR_FLAGS \
--attribute author="${setup.config.contact-information.name}" \
--attribute homepage="${import ./base-url.nix setup.config.base-url}[${setup.config.contact-information.homepage-label}]" \
--attribute imagesdir="${import ./base-url.nix setup.config.base-url}/blog/raw/$ARTICLE_NAME" \
--backend html5 \
--attribute nofooter \
--attribute webfonts!
sed -i 's/^<head>$/<head>\n<base target="_parent">/' "static/$ARTICLE_NAME.html"
echo "PDF: '$ARTICLE_NAME'"
asciidoctor-pdf "source/$ARTICLE_NAME.adoc" --out-file "static/$ARTICLE_NAME.pdf" $ASCIIDOCTOR_FLAGS \
--attribute author="${setup.config.contact-information.name}" \
--attribute homepage="${import ./base-url.nix setup.config.base-url}[${setup.config.contact-information.homepage-label}]" \
--attribute imagesdir="$ARTICLE_NAME" \
--attribute pdf-theme="pdf-theme.yml"
if [ -d "source/$ARTICLE_NAME" ]
then
echo "Additional directory exists: '$ARTICLE_NAME'"
cp -r source/$ARTICLE_NAME static
else
echo "Additional directory doesn't exist: '$ARTICLE_NAME'"
fi
else
echo "Article doesn't exist: '$ARTICLE_NAME'"
exit 2
echo "Additional directory exists: '$ARTICLE_NAME'"
cp -r source/$ARTICLE_NAME build
else
echo "Additional directory doesn't exist: '$ARTICLE_NAME'"
fi
}
Expand All @@ -61,7 +31,7 @@
done
'';
installPhase = ''
cp --recursive static $out
cp --recursive build $out
'';
buildInputs = [
];
Expand All @@ -79,27 +49,6 @@
pkgs.mkShell {
packages = [
pkgs.asciidoctor
(
writeScriptBin "asciidoctor-pdf-blog" ''
ARTICLE_NAME="$1"
ASCIIDOCTOR_FLAG_LIST=(
"--doctype article"
"--safe-mode server"
"--attribute source-highlighter=rouge"
"--attribute prewrap!"
"--attribute email=${setup.config.contact-information.email-address}"
"--attribute revnumber="unknown-revision""
)
ASCIIDOCTOR_FLAGS="$(for flag in "''${ASCIIDOCTOR_FLAG_LIST[*]}"; do echo $flag; done)"
asciidoctor-pdf "source/$ARTICLE_NAME.adoc" --out-file "out/$ARTICLE_NAME.pdf" $ASCIIDOCTOR_FLAGS \
--attribute author="${setup.config.contact-information.name}" \
--attribute homepage="${import ./base-url.nix setup.config.base-url}[${setup.config.contact-information.homepage-label}]" \
--attribute imagesdir="$ARTICLE_NAME" \
--attribute pdf-theme="pdf-theme.yml"
''
)
];
};

Expand Down

0 comments on commit dce92bf

Please sign in to comment.