Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configuration
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Binary dependencies
export DENO=v1.28.2
export DENO_DOM=v0.1.35-alpha-artifacts
export PANDOC=3.1.1
export PANDOC=3.1.2
export DARTSASS=1.55.0
export ESBUILD=0.15.6

Expand Down
5 changes: 3 additions & 2 deletions package/src/common/dependencies/pandoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function pandoc(version: string): Dependency {
`https://github.com/jgm/pandoc/releases/download/${version}/${filename}`,
configure: async (config: Configuration, path: string) => {
const dir = dirname(path);
const pandocSubdir = join(dir, `pandoc-${version}`);
// TODO: deal with aarch64 pandoc
const pandocSubdir = join(dir, `pandoc-${version}${(config.os === "darwin" ) ? ("-" + "x86_64") : ""}`);
const vendor = Deno.env.get("QUARTO_VENDOR_BINARIES");
if (vendor === undefined || vendor === "true") {
// Clean pandoc interim dir
Expand Down Expand Up @@ -85,7 +86,7 @@ export function pandoc(version: string): Dependency {
"pandoc",
),
"darwin": pandocRelease(
`pandoc-${version}-macOS.zip`,
`pandoc-${version}-x86_64-macOS.zip`,
"pandoc",
),
},
Expand Down
7 changes: 5 additions & 2 deletions src/command/render/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { figuresDir, inputFilesDir } from "../../core/render.ts";

import { Format } from "../../config/types.ts";
import { isHtmlFileOutput, isLatexOutput } from "../../config/format.ts";
import { kKeepMd, kKeepTex } from "../../config/constants.ts";
import { kKeepMd, kKeepTex, kKeepTyp } from "../../config/constants.ts";

import { filesDirLibDir, filesDirMediabagDir } from "./render-paths.ts";

Expand Down Expand Up @@ -48,7 +48,10 @@ export function renderCleanup(
// if we aren't keeping the markdown or text and we are instructed to
// clean supporting files then do it
if (
!format.execute[kKeepMd] && !format.render[kKeepTex] && supporting
!format.execute[kKeepMd] &&
!format.render[kKeepTex] &&
!format.render[kKeepTyp] &&
supporting
) {
// ammend supporting with lib dir (if it exists) for html formats
if (isHtmlFileOutput(format.pandoc)) {
Expand Down
24 changes: 8 additions & 16 deletions src/command/render/output-tex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

import { dirname, isAbsolute, join, normalize, relative } from "path/mod.ts";
import { dirname, join, normalize, relative } from "path/mod.ts";
import { ensureDirSync } from "fs/mod.ts";

import { writeFileToStdout } from "../../core/console.ts";
Expand All @@ -21,6 +21,7 @@ import { OutputRecipe } from "./types.ts";
import { pdfEngine } from "../../config/pdf.ts";
import { execProcess } from "../../core/process.ts";
import { parseFormatString } from "../../core/pandoc/pandoc-formats.ts";
import { normalizeOutputPath } from "./output.ts";

export interface PdfGenerator {
generate: (
Expand Down Expand Up @@ -109,15 +110,17 @@ export function texToPdfOutputRecipe(

// final output needs to either absolute or input dir relative
// (however it may be working dir relative when it is passed in)
return texNormalizePath(input, finalOutput);
return normalizeOutputPath(input, finalOutput);
} else {
return texNormalizePath(input, pdfOutput);
return normalizeOutputPath(input, pdfOutput);
}
};

const pdfOutput = finalOutput
? finalOutput === kStdOut ? undefined : texNormalizePath(input, finalOutput)
: texNormalizePath(input, pdfGenerator.computePath(input, format));
? finalOutput === kStdOut
? undefined
: normalizeOutputPath(input, finalOutput)
: normalizeOutputPath(input, pdfGenerator.computePath(input, format));

// tweak writer if it's pdf
const to = format.pandoc.to === "pdf" ? pdfIntermediateTo : format.pandoc.to;
Expand Down Expand Up @@ -209,14 +212,3 @@ export function contextPdfOutputRecipe(
},
);
}

const texNormalizePath = (input: string, output: string) => {
if (isAbsolute(output)) {
return output;
} else {
return relative(
dirname(input),
output,
);
}
};
101 changes: 101 additions & 0 deletions src/command/render/output-typst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* output-typst.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*
*/

import { dirname, join, normalize, relative } from "path/mod.ts";
import { ensureDirSync } from "fs/mod.ts";

import { kKeepTyp, kOutputExt, kOutputFile } from "../../config/constants.ts";
import { Format } from "../../config/types.ts";
import { writeFileToStdout } from "../../core/console.ts";
import { dirAndStem, expandPath } from "../../core/path.ts";
import { kStdOut, replacePandocOutputArg } from "./flags.ts";
import { OutputRecipe, RenderOptions } from "./types.ts";
import { normalizeOutputPath } from "./output.ts";
import { typstCompile } from "../../core/typst.ts";

export function useTypstPdfOutputRecipe(
format: Format,
) {
return format.pandoc.to === "typst" &&
format.render[kOutputExt] === "pdf";
}

export function typstPdfOutputRecipe(
input: string,
finalOutput: string,
options: RenderOptions,
format: Format,
): OutputRecipe {
// cacluate output and args for pandoc (this is an intermediate file
// which we will then compile to a pdf and rename to .typ)
const [inputDir, inputStem] = dirAndStem(input);
const output = inputStem + ".typ";
let args = options.pandocArgs || [];
const pandoc = { ...format.pandoc };
if (options.flags?.output) {
args = replacePandocOutputArg(args, output);
} else {
pandoc[kOutputFile] = output;
}

// when pandoc is done, we need to run the pdf generator and then copy the
// ouptut to the user's requested destination
const complete = async () => {
// input file is pandoc's output
const input = join(inputDir, output);

// run typst
const pdfOutput = join(inputDir, inputStem + ".pdf");
const result = await typstCompile(input, pdfOutput, options.flags?.quiet);
if (!result.success) {
throw new Error();
}

// keep typ if requested
if (!format.render[kKeepTyp]) {
Deno.removeSync(input);
}

// copy (or write for stdout) compiled pdf to final output location
if (finalOutput) {
if (finalOutput === kStdOut) {
writeFileToStdout(pdfOutput);
Deno.removeSync(pdfOutput);
} else {
const outputPdf = expandPath(finalOutput);

if (normalize(pdfOutput) !== normalize(outputPdf)) {
// ensure the target directory exists
ensureDirSync(dirname(outputPdf));
Deno.renameSync(pdfOutput, outputPdf);
}
}

// final output needs to either absolute or input dir relative
// (however it may be working dir relative when it is passed in)
return normalizeOutputPath(input, finalOutput);
} else {
return normalizeOutputPath(input, pdfOutput);
}
};

const pdfOutput = finalOutput
? finalOutput === kStdOut
? undefined
: normalizeOutputPath(input, finalOutput)
: normalizeOutputPath(input, join(inputDir, inputStem + ".pdf"));

// return recipe
return {
output,
keepYaml: false,
args,
format: { ...format, pandoc },
complete,
finalOutput: pdfOutput ? relative(inputDir, pdfOutput) : undefined,
};
}
17 changes: 17 additions & 0 deletions src/command/render/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ import {
} from "./output-tex.ts";
import { formatOutputFile } from "../../core/render.ts";
import { kYamlMetadataBlock } from "../../core/pandoc/pandoc-formats.ts";
import {
typstPdfOutputRecipe,
useTypstPdfOutputRecipe,
} from "./output-typst.ts";

// render commands imply the --output argument for pandoc and the final
// output file to create for the user, but we need a 'recipe' to go from
Expand Down Expand Up @@ -77,6 +81,8 @@ export function outputRecipe(
return quartoLatexmkOutputRecipe(input, output, options, format);
} else if (useContextPdfOutputRecipe(format, options.flags)) {
return contextPdfOutputRecipe(input, output, options, format);
} else if (useTypstPdfOutputRecipe(format)) {
return typstPdfOutputRecipe(input, output, options, format);
} else {
// default recipe spec based on user input
const completeActions: VoidFunction[] = [];
Expand Down Expand Up @@ -198,3 +204,14 @@ export function outputRecipe(
return recipe;
}
}

export function normalizeOutputPath(input: string, output: string) {
if (isAbsolute(output)) {
return output;
} else {
return relative(
dirname(input),
output,
);
}
}
2 changes: 2 additions & 0 deletions src/command/render/pandoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
isIpynbOutput,
isLatexOutput,
isMarkdownOutput,
isTypstOutput,
} from "../../config/format.ts";
import {
isIncludeMetadata,
Expand Down Expand Up @@ -735,6 +736,7 @@ export async function runPandoc(
// crossref filter so we only do this if the user hasn't disabled the crossref filter
if (
!isLatexOutput(options.format.pandoc) &&
!isTypstOutput(options.format.pandoc) &&
!isMarkdownOutput(options.format) && crossrefFilterActive(options)
) {
delete allDefaults[kNumberSections];
Expand Down
3 changes: 3 additions & 0 deletions src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const kShortcodes = "shortcodes";

export const kKeepMd = "keep-md";
export const kKeepTex = "keep-tex";
export const kKeepTyp = "keep-typ";
export const kKeepIpynb = "keep-ipynb";
export const kKeepSource = "keep-source";
export const kVariant = "variant";
Expand Down Expand Up @@ -140,6 +141,7 @@ export const kExecuteDefaultsKeys = [

export const kRenderDefaultsKeys = [
kKeepTex,
kKeepTyp,
kKeepSource,
kKeepHidden,
kVariant,
Expand Down Expand Up @@ -429,6 +431,7 @@ export const kPdfEngineOpts = "pdf-engine-opts";
export const kPdfEngineOpt = "pdf-engine-opt";
export const kListings = "listings";
export const kNumberSections = "number-sections";
export const kSectionNumbering = "section-numbering";
export const kNumberOffset = "number-offset";
export const kShiftHeadingLevelBy = "shift-heading-level-by";
export const kNumberDepth = "number-depth";
Expand Down
9 changes: 9 additions & 0 deletions src/config/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ export function isLatexOutput(format: FormatPandoc) {
return ["pdf", "latex", "beamer"].includes(format.to || "");
}

export function isTypstOutput(format: string): boolean;
export function isTypstOutput(format: FormatPandoc): boolean;
export function isTypstOutput(format: string | FormatPandoc) {
if (typeof (format) !== "string") {
format = format?.to || "html";
}
return format === "typst";
}

export function isBeamerOutput(format: FormatPandoc) {
return ["beamer"].includes(format.to || "");
}
Expand Down
2 changes: 2 additions & 0 deletions src/config/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
kIpynbFilters,
kKeepMd,
kKeepTex,
kKeepTyp,
kLanguageDefaults,
kLanguageDefaultsKeys,
kMetadataFile,
Expand Down Expand Up @@ -126,6 +127,7 @@ export function formatFromMetadata(
if (debug) {
mergedFormat.execute[kKeepMd] = true;
mergedFormat.render[kKeepTex] = true;
mergedFormat.render[kKeepTyp] = true;
}

return mergedFormat;
Expand Down
2 changes: 2 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import {
kKeepMd,
kKeepSource,
kKeepTex,
kKeepTyp,
kLatexAutoInstall,
kLatexAutoMk,
kLatexClean,
Expand Down Expand Up @@ -378,6 +379,7 @@ export interface Format {

export interface FormatRender {
[kKeepTex]?: boolean;
[kKeepTyp]?: boolean;
[kKeepSource]?: boolean;
[kKeepHidden]?: boolean;
[kPreferHtml]?: boolean;
Expand Down
6 changes: 5 additions & 1 deletion src/core/handlers/dot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isJavascriptCompatible,
isLatexOutput,
isRevealjsOutput,
isTypstOutput,
} from "../../config/format.ts";
import { QuartoMdCell } from "../lib/break-quarto-md.ts";
import { mappedConcat, mappedIndexToLineCol } from "../lib/mapped-text.ts";
Expand Down Expand Up @@ -106,6 +107,9 @@ const dotHandler: LanguageHandler = {
isLatexOutput(handlerContext.options.format.pandoc)
? ` fig-env='${cell.options?.["fig-env"] || "figure"}'`
: "";
const heightOffset = isTypstOutput(handlerContext.options.format.pandoc)
? 0.1
: 0.0;
let posSpecifier = "";
if (
isLatexOutput(handlerContext.options.format.pandoc) &&
Expand All @@ -123,7 +127,7 @@ const dotHandler: LanguageHandler = {
? `width="${Math.round(width * 100) / 100}in"`
: "";
const heightSpecifier = height
? ` height="${Math.round(height * 100) / 100}in"`
? ` height="${(Math.round(height * 100) / 100) + heightOffset}in"`
: "";
const captionSpecifier = includeCaption
? (cell.options?.["fig-cap"] || "")
Expand Down
1 change: 1 addition & 0 deletions src/core/mime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,5 @@ const MEDIA_TYPES: Record<string, string> = {
".mediawiki": kTextPlain,
".xwiki": kTextPlain,
".zim": kTextPlain,
".typ": kTextPlain,
};
Loading