@abreen/tada 1.8.1 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/validators.ts CHANGED
@@ -80,6 +80,6 @@ export function createSiteConfig({
80
80
  themeColor,
81
81
  tintHue: Number(tintHue),
82
82
  tintAmount: Number(tintAmount),
83
- vars: {},
83
+ vars: { foobar: 123 },
84
84
  };
85
85
  }
@@ -122,8 +122,6 @@ export function compileTemplates(
122
122
  _.template(fs.readFileSync(filePath, 'utf-8'))({
123
123
  vars: siteVariables.vars || {},
124
124
  site: siteVariables,
125
- base: siteVariables.base,
126
- basePath: siteVariables.basePath,
127
125
  }),
128
126
  );
129
127
 
@@ -24,6 +24,7 @@ import {
24
24
  toPosix,
25
25
  toUrlPath,
26
26
  } from './paths';
27
+ import { applySourceTemplate } from './source-template';
27
28
  import pkg from '../../package.json' with { type: 'json' };
28
29
  import { parseFrontMatterAndContent } from './front-matter';
29
30
  import { createMarkdown } from './markdown';
@@ -130,8 +131,6 @@ function createTemplateParameters({
130
131
  vars: siteVariables.vars || {},
131
132
  ...createGlobals(pageVariables, siteVariables, subPath),
132
133
  site: siteVariables,
133
- base: siteVariables.base,
134
- basePath: siteVariables.basePath,
135
134
  page: pageVariables,
136
135
  content,
137
136
  applyBasePath,
@@ -282,7 +281,8 @@ export function renderCodePageAsset({
282
281
  const subPath = toPosix(path.relative(contentDir, path.join(dir, name)));
283
282
  const applyBasePath = createApplyBasePath(siteVariables);
284
283
  const lang = siteVariables.codeLanguages![ext.slice(1).toLowerCase()];
285
- const sourceCode = fs.readFileSync(filePath, 'utf-8');
284
+ const rawSource = fs.readFileSync(filePath, 'utf-8');
285
+ const sourceCode = applySourceTemplate(rawSource, siteVariables, filePath);
286
286
  const pageDirPath = toPosix(path.relative(contentDir, dir));
287
287
 
288
288
  log.info`Rendering code page ${B`${subPath + ext}`}`;
@@ -346,20 +346,26 @@ export function renderCopiedContentAsset({
346
346
 
347
347
  log.info`${label} ${B`${relPath}`}`;
348
348
 
349
- if (filePath.endsWith('.java') && isFeatureEnabled(siteVariables, 'code')) {
350
- const sourceCode = fs.readFileSync(filePath, 'utf-8');
349
+ if (!isFeatureEnabled(siteVariables, 'code')) {
350
+ return [{ assetPath: relPath, content: fs.readFileSync(filePath) }];
351
+ }
352
+
353
+ const rawSource = fs.readFileSync(filePath, 'utf-8');
354
+ const templated = applySourceTemplate(rawSource, siteVariables, filePath);
355
+
356
+ if (filePath.endsWith('.java')) {
351
357
  const pageDirPath = toPosix(
352
358
  path.relative(contentDir, path.dirname(filePath)),
353
359
  );
354
360
  const rewrittenLines = rewriteProseLinks(
355
- sourceCode.split('\n'),
361
+ templated.split('\n'),
356
362
  siteVariables,
357
363
  pageDirPath,
358
364
  );
359
365
  return [{ assetPath: relPath, content: rewrittenLines.join('\n') }];
360
366
  }
361
367
 
362
- return [{ assetPath: relPath, content: fs.readFileSync(filePath) }];
368
+ return [{ assetPath: relPath, content: templated }];
363
369
  }
364
370
 
365
371
  /** Parses the file, renders using template, returns HTML & params used to generate page */
@@ -0,0 +1,27 @@
1
+ import _ from 'lodash';
2
+ import type { SiteVariables } from '../types';
3
+
4
+ /**
5
+ * Applies Lodash template syntax to a source code file's contents so that
6
+ * authors can interpolate site variables (like `<%= vars.fullCourseName %>`)
7
+ * in their source code. The template context mirrors the one used for JSON
8
+ * data files in `build/templates.ts`: `vars` and `site`. Invoked from
9
+ * `renderCodePageAsset` and `renderCopiedContentAsset` only when
10
+ * `features.code` is enabled.
11
+ */
12
+ export function applySourceTemplate(
13
+ source: string,
14
+ siteVariables: SiteVariables,
15
+ filePath: string,
16
+ ): string {
17
+ const params = { vars: siteVariables.vars || {}, site: siteVariables };
18
+
19
+ try {
20
+ return _.template(source)(params);
21
+ } catch (err: unknown) {
22
+ throw new Error(
23
+ `${filePath}: Lodash template error in source code: ${(err as Error).message}`,
24
+ { cause: err },
25
+ );
26
+ }
27
+ }
@@ -4,6 +4,8 @@
4
4
  /// by scaling using a _scale factor_ (a floating-point number between 0 and 1).
5
5
  ///
6
6
  /// See [`rectangle.py`](./rectangle.py) for a Python version of this class.
7
+ ///
8
+ /// Here's `vars.foobar`: <%= vars.foobar %>
7
9
  public class Rectangle {
8
10
  private int width;
9
11
  private int height;
@@ -8,6 +8,12 @@ author: alex
8
8
 
9
9
  Here is a sample description of the topics covered at the first lecture.
10
10
 
11
+ The value of `vars.foobar` is <%= vars.foobar %>.
12
+
13
+ You can add any variables you want to the `site.dev.json` and `site.prod.json`
14
+ files under the `"vars"` property and access them using Lodash template syntax
15
+ from Markdown files, HTML files, and source code. (Lodash template subtitution
16
+ only works in source code files when `features.code` is `true`.)
11
17
 
12
18
  ## Slides
13
19
 
@@ -1,3 +1,8 @@
1
+ #
2
+ # rectangle.py
3
+ #
4
+ # Here's vars.foobar: <%= vars.foobar %>
5
+ #
1
6
  class Rectangle:
2
7
  """A rectangle with integer side lengths.
3
8
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abreen/tada",
3
- "version": "1.8.1",
3
+ "version": "1.9.0",
4
4
  "type": "module",
5
5
  "description": "A static site generator",
6
6
  "license": "MIT",