Compare commits

...

6 Commits

7 changed files with 38 additions and 60 deletions

View File

@ -52,7 +52,7 @@ func newConfig() *Config {
MaxImgWidth: 1920,
PDFDir: "/var/www/cpolis/pdfs",
Port: ":1664",
Version: "v0.15.4",
Version: "v0.16.0",
WebDir: "/var/www/cpolis/web",
}
}

View File

@ -21,7 +21,7 @@ func ConvertToMarkdown(c *Config, filename string) ([]byte, error) {
defer os.RemoveAll(tmpDir)
articleFileName := filepath.Join(os.TempDir(), fmt.Sprint(uuid.New(), ".md"))
cmd := exec.Command("pandoc", "-s", "-f", "docx", "-t", "commonmark_x", "-o", articleFileName, "--extract-media", tmpDir, filename) // TODO: Is writing to a file necessary?
cmd := exec.Command("pandoc", "-s", "-L", filepath.Join("scripts", "create_toc.lua"), "-f", "docx", "-t", "commonmark_x", "-o", articleFileName, "--extract-media", tmpDir, filename) // TODO: Is writing to a file necessary?
cmd.Stderr = &stderr
if err = cmd.Run(); err != nil {
return nil, fmt.Errorf("error converting docx to markdown: %v: %v", err, stderr.String())
@ -33,6 +33,9 @@ func ConvertToMarkdown(c *Config, filename string) ([]byte, error) {
return nil, fmt.Errorf("error reading markdown file: %v", err)
}
re := regexp.MustCompile(`\{width=[^}]+height=[^}]+\}`)
articleContent = re.ReplaceAll(articleContent, []byte(""))
imageNames, err := filepath.Glob(filepath.Join(tmpDir, "media", "*"))
if err != nil {
return nil, fmt.Errorf("error getting docx images from temporary directory: %v", err)

33
scripts/create_toc.lua Normal file
View File

@ -0,0 +1,33 @@
-- Helper function: remove all image inlines from a list of inlines.
local function remove_images(inlines)
local result = {}
for _, item in ipairs(inlines) do
if item.t ~= "Image" then
table.insert(result, item)
end
end
return result
end
-- Build a bullet list representing the table of contents.
local function build_toc(doc)
local toc_items = {}
for _, block in ipairs(doc.blocks) do
if block.t == "Header" then
local clean_inlines = remove_images(block.content)
local header_text = pandoc.utils.stringify(clean_inlines)
if header_text ~= "" then
local link = pandoc.Link(clean_inlines, "#" .. block.identifier)
table.insert(toc_items, { link })
end
end
end
return pandoc.BulletList(toc_items)
end
-- The Pandoc function runs after the document is fully constructed.
function Pandoc(doc)
local toc = build_toc(doc)
table.insert(doc.blocks, 1, toc) -- Insert the TOC at the very beginning of the document.
return doc
end

View File

@ -1,58 +0,0 @@
#! /bin/sh -
CPOLIS_REPO_URL="https://git.streifling.com/api/v1/repos/jason/cpolis/releases"
EXTRACTION_DIR=$HOME
CPOLIS_DIR=$EXTRACTION_DIR/cpolis
TAILWINDCSS_REPO_URL=https://api.github.com/repos/tailwindlabs/tailwindcss/releases/latest
TMP_DIR=/tmp
BIN_DIR=/usr/local/bin
SYSTEMD_DIR=/etc/systemd/system
check_dependency() {
if ! which $1 >/dev/null 2>&1; then
echo "$1 needs to be installed" >&2
exit 1
fi
}
if ! groups | grep -E 'root|wheel|sudo' >/dev/null; then
echo "You need administrative privileges for this script" >&2
exit 1
fi
check_dependency curl
check_dependency go
check_dependency jq
check_dependency tar
check_dependency xargs
echo '\nDownloading cpolis...' >&2
rm -fr $CPOLIS_DIR/*
latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name')
curl -Lo $TMP_DIR/cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz
tar -xzf $TMP_DIR/cpolis.tar.gz -C $EXTRACTION_DIR
rm $TMP_DIR/cpolis.tar.gz
echo '\nDownloading TailwindCSS...' >&2
curl -s $TAILWINDCSS_REPO_URL |
grep -F browser_download_url |
grep -F linux-x64 |
cut -d'"' -f4 |
xargs -r curl -Lo $CPOLIS_DIR/tailwindcss
chmod +x $CPOLIS_DIR/tailwindcss
$CPOLIS_DIR/tailwindcss -i $CPOLIS_DIR/web/static/css/input.css -o $CPOLIS_DIR/web/static/css/style.css
echo '\nBuilding cpolis...' >&2
cd $CPOLIS_DIR
go build -o $BIN_DIR/cpolis cmd/main.go
cd
echo '\nSetting up system files...' >&2
sudo chown root:root $BIN_DIR/cpolis
chmod +x $BIN_DIR/cpolis
echo '\nSetting up service...' >&2
sudo mv $CPOLIS_DIR/cpolis.service $SYSTEMD_DIR
sudo chown root:root $SYSTEMD_DIR/cpolis.service
sudo systemctl daemon-reload
sudo systemctl is-active --quiet cpolis.service && sudo systemctl restart cpolis.service