5
0
Fork 0
hedgedoc2pdf/pandocPipeline/pandocPipeline.bat
2023-10-10 14:24:00 +02:00

130 lines
3 KiB
Batchfile

@echo off
::if no arguments given show help
IF "%1"=="" (goto :help)
set editor=%USERNAME%
set title =
set file = none
set ending = tex
set "_="
::path to this script
set script= %~dp0
set version= 1.0
::parsing arguments
:loop
IF NOT "%1"=="" (
IF "%1"=="-h" (
goto :help
)
IF "%1"=="--help" (
goto :help
)
IF "%1"=="-v" (
goto :version
)
IF "%1"=="--version" (
goto :version
)
IF "%1"=="-f" (
SET file=%2
SHIFT
)
IF "%1"=="--file" (
SET file=%2
SHIFT
)
IF "%1"=="-e" (
SET ending=%2
SHIFT
)
IF "%1"=="--ending" (
SET ending=%2
SHIFT
)
SHIFT
GOTO :loop
)
:program
::check if file to parse was given
IF %file%==none (goto :errorfile) ELSE (goto :extract_metadata)
:extract_metadata
set title=%file%
For /F "UseBackQ Tokens=1,*" %%G In ("%file%.md") Do (
If "%%~G" == "[Title]:" (
set title=%%H
)
If "%%~G" == "[Author]:" (
set editor=%%H
)
)
set title=%title:~3,-1%
set editor=%editor:~3,-1%
goto :selection
:selection
IF %ending%==pdf (goto :program_tex)
IF %ending%==docx (goto :program_docx) ELSE (goto :program_other)
:program_tex
set header=%script%header.md
set header=%header: =%
set sedfile=%script%body.sed
set sedfile=%sedfile: =%
set picture=%script%Logo.png
set picture=%picture: =%
set picture=%picture:\=/%
set pandocenv=%script%pandoc_latex_environment.py
set pandocenv=%pandocenv: =%
::fill header with appropriate information
sed -i 's,Editor:.*,Editor: '"%editor%"' \\\\\\\\,' %header%
sed -i 's,Title:.*,Title: '"%title%"',' %header%
sed -i 's,textwidth]{.*,textwidth]{'%picture%'}},' %header%
::execute pandoc
cat %file%.md %header% | sed -E -f %sedfile%| pandoc --filter %pandocenv% --number-sections --toc -i - -o %file%.pdf
goto :open
:program_docx
set refdoc=%script%custom-reference.docx
set refdoc=%refdoc: =%
pandoc %file%.md --reference-doc=%refdoc% -f markdown -t %ending% -s -o %file%.%ending%
goto :open
:program_other
pandoc %file%.md -f markdown -t %ending% -s -o %file%.%ending%
goto :open
:open
%file%.%ending%
goto :end
:help
echo Tool to convert file from markdown to another format
echo Usage: pandocPipeline [OPTION] [COMMAND]
echo -h, --help show this help screen
echo -f, --file specify filename without extension
echo -e, --ending spefify target file format
echo Pipeline is optimized for target format pdf and docx but works with others as well
echo Possible fileformats see: https://pandoc.org/diagram.svgz?v=20230203095535
echo Author is by default %USERNAME%
echo to change the author write follwing in your markdown file
echo [Author]: # (your name)
echo Title is by default the filename
echo to change the title write follwing in your markdown file
echo [Title]: # (document title)
goto :end
:version
echo pandocPipeline %version%
goto :end
:errorfile
echo No file was given
echo Please add a filename to your command
echo Try 'pandocPipeline --help' for more information
goto :end
:end