Export md-files to PDF via Weave

Yesterday I struggled to export md-files via package Weave to pdf.
Fist mistake was a search for a portable package for XeLaTeX,
it took a while to understand that it is the best to install MiKTeX.
As XeLaTeX is incorporated inside this ecosystem.
As a result I wrote an example to export md-files of a specific folder into
a specified export folder, I hope it helps other to save time to getting started.

P.S.:
Is there a way to make sure a julia-script is executed inside the command line interface (CLI) / outside VSCODE-REPL?

# --------------------------------------------------------------------------------------------------------------------------
# --- important: run from command linne interface (CLI) / outside vscode-REPL
# --------------------------------------------------------------------------------------------------------------------------
# include(raw"C:\data\git_repos\my_julia_stuff\julia_scripts\example_export_markdown_via_weave2pdf.jl")
# --- inital checks for insstalled packages: -------------------------------------------------------------------------------
import Pkg
if isdir(Pkg.dir("Weave"))
    Pkg.add("Weave")
end
# --- main:
using Weave
# --- install XeLaTeX as part of MiKTex ------------------------------------------------------------------------------------
# install XeLaTeX: http://www.texts.io/support/0002/#:~:text=To%20install%20XeLaTeX%20please%20download,PDF%20files%20in%20one%20click.
# MiKTeX portable: https://miktex.org/howto/portable-edition
# --- be shure "XeLaTeX.exe" can be found, add executable dir to $PATH$: ---------------------------------------------------
dir_XeLaTeX = raw"C:\bin\miktex-portable\texmfs\install\miktex\bin\x64"
if ~ispath(dir_XeLaTeX)
    error(string("\"", dir_XeLaTeX, "\" does not exist!"))
end
if ~occursin(dir_XeLaTeX, ENV["path"])
    ENV["path"] = string(ENV["path"], string(dir_XeLaTeX, ";"))
    println("\"", string(dir_XeLaTeX, ";"), "\" added to %path% !")
end

# --- example to export all markdown / md-files in one directory: ----------------------------------------------------------
dir_export      = raw"C:\tmp\plt\plotlyjs"
dir_source      = raw"C:\Users\stefanpofahl\.julia\packages\PlotlyJS\Jj38U\docs\src"
if ~ispath(dir_export)
    mkpath(dir_export)
end
if ~ispath(dir_export)
    error(string("\"", dir_export, "\" does not exist!"))
end
if ~ispath(dir_source)
    error(string("\"", dir_source, "\" does not exist!"))
end
# --- list all file in one directory, export each *.md inside this directory -----------------------------------------------
fn_list = readdir(dir_source, join=true)
for i_file in fn_list
    fn_body_full, fn_ext = splitext(i_file)
    if cmp(fn_ext, ".md") == 0
        _, fn_body      = splitdir(fn_body_full)
        fn_output_pdf   = joinpath(dir_export, string(fn_body, ".pdf"))
        println(fn_output_pdf)
        if ~isfile(fn_output_pdf)
            weave(i_file; doctype = "md2pdf", out_path = dir_export)
        end
    end
end
@info(string("PDF exported to: \"", dir_export, "\""))
1 Like