Skip to content

Commit

Permalink
directives -> shortcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
cscheid committed Apr 28, 2022
1 parent be60597 commit 51e34b4
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 804 deletions.
11 changes: 3 additions & 8 deletions src/core/handlers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export async function handleLanguageCells(
// directive handler
for (const cell of cells) {
const directiveCellType = cell.source.cell_type as DirectiveCell;
const innerLanguage = directiveCellType.tag;
const innerLanguage = directiveCellType.name;
const innerLanguageHandler = handlers[innerLanguage]!;

if (
Expand All @@ -235,12 +235,7 @@ export async function handleLanguageCells(
innerLanguageHandler.type === "cell"
) {
// if no handler is present (or a directive was included for something
// that responds to cells instead), just create a div tag
newCells[cell.index] = mappedReplace(
cell.source.source,
new RegExp(`<${directiveCellType.tag}`, "i"),
`<div quarto-directive="${directiveCellType.tag}"`,
);
// that responds to cells instead), we're a no-op
continue;
}
if (innerLanguageHandler.directive === undefined) {
Expand All @@ -257,7 +252,7 @@ export async function handleLanguageCells(

newCells[cell.index] = asMappedString(innerLanguageHandler.directive(
innerHandler.context,
directiveCellType.attrs,
directiveCellType,
));

results = mergeConfigs(results, innerHandler.results);
Expand Down
38 changes: 23 additions & 15 deletions src/core/handlers/include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import {
import { dirname, join, normalize, relative } from "path/mod.ts";
import { encodeMetadata } from "../encode-metadata.ts";
import { rangedLines } from "../lib/ranged-text.ts";
import { isDirectiveTag } from "../lib/parse-directive-tag.ts";
import {
getShortcodeNamedParams,
getShortcodeUnnamedParams,
isBlockShortcode,
} from "../lib/parse-shortcode.ts";
import { DirectiveCell } from "../lib/break-quarto-md-types.ts";

const includeHandler: LanguageHandler = {
...baseHandler,
Expand All @@ -29,7 +34,7 @@ const includeHandler: LanguageHandler = {

directive(
handlerContext: LanguageCellHandlerContext,
options: Record<string, string>,
directive: DirectiveCell,
) {
const sourceDir = dirname(handlerContext.options.source);
const retrievedFiles: string[] = [handlerContext.options.source];
Expand Down Expand Up @@ -94,20 +99,24 @@ const includeHandler: LanguageHandler = {

let rangeStart = 0;
for (const { substring, range } of rangedLines(includeSrc.value)) {
const m = isDirectiveTag(substring);
const m = isBlockShortcode(substring);
if (m && m.name.toLocaleLowerCase() === "include") {
textFragments.push(
mappedString(includeSrc, [{ start: rangeStart, end: range.start }]),
);
rangeStart = range.end;
if (typeof m.attributes.file !== "string") {
throw new Error("Include directive needs attribute `file`");
const params = getShortcodeUnnamedParams(m);
const options = getShortcodeNamedParams(m);
if (params.length === 0) {
throw new Error("Include directive needs file parameter");
}
const fixup = m.attributes.fixup === undefined
const file = params[0];
const fixup = options.fixup === undefined
? undefined
: ((m.attributes.fixup as string).toLocaleLowerCase() !== "false");
: (options.fixup.toLocaleLowerCase() !== "false");

retrieveInclude(
join(...[...retrievedDirectories, m.attributes.file]),
join(...[...retrievedDirectories, file]),
fixup,
);
}
Expand All @@ -128,17 +137,16 @@ const includeHandler: LanguageHandler = {
addFixup(filename);
};

const fileName = options?.["file"];
if (fileName === undefined) {
throw new Error("Include directive needs attribute `file`");
const params = getShortcodeUnnamedParams(directive);
const options = getShortcodeNamedParams(directive);
if (params.length === 0) {
throw new Error("Include directive needs filename as a parameter");
}

const includeName = join(sourceDir, fileName as string);
const includeName = join(sourceDir, params[0]);

const fixup = options.fixup === undefined
? undefined
: ((typeof options.fixup === "string") &&
(options.fixup.toLocaleLowerCase() !== "false"));
: (options.fixup.toLocaleLowerCase() !== "false");

retrieveInclude(includeName, fixup);

Expand Down
3 changes: 2 additions & 1 deletion src/core/handlers/pagebreak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { LanguageCellHandlerContext, LanguageHandler } from "./types.ts";
import { baseHandler, install } from "./base.ts";
import { DirectiveCell } from "../lib/break-quarto-md-types.ts";

const includeHandler: LanguageHandler = {
...baseHandler,
Expand All @@ -17,7 +18,7 @@ const includeHandler: LanguageHandler = {

directive(
_handlerContext: LanguageCellHandlerContext,
_options: Record<string, string>,
_directive: DirectiveCell,
) {
return "\n\n\\pagebreak\n\n";
},
Expand Down
4 changes: 2 additions & 2 deletions src/core/handlers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "../../config/constants.ts";
import { DependencyFile, Format, FormatExtras } from "../../config/types.ts";
import { PandocIncludes } from "../../execute/types.ts";
import { QuartoMdCell } from "../lib/break-quarto-md-types.ts";
import { DirectiveCell, QuartoMdCell } from "../lib/break-quarto-md-types.ts";
import { EitherString, MappedString } from "../lib/text-types.ts";
import { ConcreteSchema } from "../lib/yaml-schema/types.ts";
import { TempContext } from "../temp-types.ts";
Expand Down Expand Up @@ -53,7 +53,7 @@ export interface LanguageHandler {

directive?: (
handlerContext: LanguageCellHandlerContext,
options: Record<string, string>,
directiveCell: DirectiveCell,
) => EitherString;

comment?: LanguageComment;
Expand Down
4 changes: 2 additions & 2 deletions src/core/lib/break-quarto-md-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export interface CodeCellType {

export interface DirectiveCell {
language: "_directive";
tag: string;
attrs: Record<string, string>;
name: string;
params: { name?: string; value: string }[];
}

export interface QuartoMdCell {
Expand Down
32 changes: 15 additions & 17 deletions src/core/lib/break-quarto-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ import { asMappedString, EitherString, mappedString } from "./mapped-text.ts";

import { partitionCellOptionsMapped } from "./partition-cell-options.ts";

import {
DirectiveCell,
QuartoMdCell,
QuartoMdChunks,
} from "./break-quarto-md-types.ts";
import { isDirectiveTag } from "./parse-directive-tag.ts";
import { QuartoMdCell, QuartoMdChunks } from "./break-quarto-md-types.ts";
import { isBlockShortcode } from "./parse-shortcode.ts";

export type { QuartoMdCell, QuartoMdChunks } from "./break-quarto-md-types.ts";

Expand All @@ -46,8 +42,13 @@ export async function breakQuartoMd(
const delimitMathBlockRegEx = /^\$\$/;

let language = ""; // current language block
let tagName = "";
let tagOptions: Record<string, string> = {}; // tag options needs to be a stack to remember the options as we close the tag
let directiveParams: {
name: string;
params: {
name?: string;
value: string;
}[];
} | undefined = undefined;
let cellStartLine = 0;

// line buffer
Expand Down Expand Up @@ -87,8 +88,8 @@ export async function breakQuartoMd(
) {
return {
language: "_directive",
tag: tagName,
attrs: tagOptions,
name: directiveParams!.name,
params: directiveParams!.params,
};
} else {
return cell_type;
Expand Down Expand Up @@ -142,8 +143,6 @@ export async function breakQuartoMd(
} else if (cell_type === "directive") {
// directives only carry tag source in sourceVerbatim, analogously to code
cell.source = mappedString(src, mappedChunks.slice(1, -1));
// directives carry options in cell_type, use that
cell.options = (cell.cell_type as DirectiveCell).attrs;
}
// if the source is empty then don't add it
if (
Expand Down Expand Up @@ -172,11 +171,11 @@ export async function breakQuartoMd(

for (let i = 0; i < srcLines.length; ++i) {
const line = srcLines[i];
const directiveMatch = isDirectiveTag(line.substring);
const directiveMatch = isBlockShortcode(line.substring);
// yaml front matter
if (
yamlRegEx.test(line.substring) && !inCodeCell && !inCode &&
!inMathBlock && tagName.length === 0
!inMathBlock
) {
if (inYaml) {
lineBuffer.push(line);
Expand All @@ -190,8 +189,7 @@ export async function breakQuartoMd(
} // found empty directive
else if (inPlainText() && directiveMatch) {
await flushLineBuffer("markdown", i);
tagName = directiveMatch.name;
tagOptions = directiveMatch.attributes;
directiveParams = directiveMatch;
lineBuffer.push(line);
await flushLineBuffer("directive", i);
} // begin code cell: ^```python
Expand Down Expand Up @@ -228,7 +226,7 @@ export async function breakQuartoMd(
lineBuffer.push(line);
await flushLineBuffer("math", i);
} else {
if (inYaml || inCode || inCodeCell || tagName.length > 0) {
if (inYaml || inCode || inCodeCell) {
// TODO signal a parse error?
// for now, we just skip.
} else {
Expand Down
Loading

0 comments on commit 51e34b4

Please sign in to comment.