@abreen/tada 1.8.0 → 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.0",
3
+ "version": "1.9.0",
4
4
  "type": "module",
5
5
  "description": "A static site generator",
6
6
  "license": "MIT",
package/src/_layout.scss CHANGED
@@ -222,8 +222,8 @@ footer {
222
222
  &::after {
223
223
  content: '';
224
224
  display: inline-block;
225
- width: 1.3em;
226
- height: 1.3em;
225
+ width: 1.5em;
226
+ height: 1.5em;
227
227
  vertical-align: middle;
228
228
  margin-left: 0.2em;
229
229
  @include contained-mask(var(--icon-tada));
@@ -16,7 +16,8 @@
16
16
  }
17
17
  }
18
18
 
19
- header {
19
+ header,
20
+ footer {
20
21
  display: none !important;
21
22
  }
22
23
 
@@ -181,7 +181,14 @@ function render(
181
181
 
182
182
  const subTitle = doc.createElement('div');
183
183
  subTitle.className = 'title';
184
- subTitle.textContent = sub.title;
184
+ if (result.template === 'code') {
185
+ subTitle.classList.add('code-page');
186
+ const code = doc.createElement('code');
187
+ code.textContent = sub.title;
188
+ subTitle.appendChild(code);
189
+ } else {
190
+ subTitle.textContent = sub.title;
191
+ }
185
192
  subA.appendChild(subTitle);
186
193
 
187
194
  if (sub.excerpt) {
@@ -1,8 +1,3 @@
1
- <% if (site.features.footer) { %>
2
- <footer>
3
- <p>Generated by <a href="https://github.com/abreen/Tada" target="_blank">Tada</a> <%= tadaVersion %></p>
4
- </footer>
5
- <% } %>
6
1
  </div>
7
2
  </body>
8
3
  </html>
@@ -0,0 +1,5 @@
1
+ <% if (site.features.footer) { %>
2
+ <footer data-pagefind-ignore>
3
+ <p>Generated by <a href="https://github.com/abreen/Tada" target="_blank">Tada</a> <%= tadaVersion %></p>
4
+ </footer>
5
+ <% } %>
@@ -67,7 +67,7 @@
67
67
  --icon-search: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24'><circle cx='11' cy='11' r='7' stroke='<%= iconColor %>' stroke-width='2' fill='none'/><line x1='16.5' y1='16.5' x2='22' y2='22' stroke='<%= iconColor %>' stroke-width='2' stroke-linecap='round'/></svg>");
68
68
  --icon-chevron: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'><polyline points='9 18 15 12 9 6' stroke='<%= iconColor %>' stroke-width='2' fill='none' stroke-linecap='round' stroke-linejoin='round'/></svg>");
69
69
  --icon-breadcrumb: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24'><path d='M3 7 H17 V22' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round' fill='none'/><polyline points='11 16 17 22 23 16' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round' fill='none'/></svg>");
70
- --icon-tada: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='39' height='39' viewBox='0 0 24 24'><path fill='black' d='m14.53 1.45l-1.08 1.08l1.6 1.6q.33.375.33.87c0 .495-.11.64-.33.86L11.5 9.47l1 1.08l3.63-3.61c.53-.59.79-1.24.79-1.94s-.26-1.36-.79-1.95zm-3.98 2.02L9.47 4.55l.61.56c.22.22.33.52.33.89s-.11.67-.33.89l-.61.56l1.08 1.08l.56-.61c.53-.59.8-1.23.8-1.92c0-.72-.27-1.37-.8-1.97zM21 5.06c-.69 0-1.33.27-1.92.8l-5.63 5.64l1.08 1l5.58-5.56c.25-.25.55-.38.89-.38s.64.13.89.38l.61.61l1.03-1.08l-.56-.61c-.59-.53-1.25-.8-1.97-.8M7 8L2 22l14-5zm12 3.06c-.7 0-1.34.27-1.94.8l-1.59 1.59l1.08 1.08l1.59-1.59c.25-.25.53-.38.86-.38s.63.13.88.38l1.62 1.59l1.05-1.03l-1.6-1.64c-.59-.53-1.25-.8-1.95-.8'/></svg>");
70
+ --icon-tada: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 -960 960 960'><path d='m121-172 151-426q4-11 13.48-17.5 9.47-6.5 20.3-6.5 7.22 0 13.52 2.5T331-612l277 276q5 5.4 7.5 11.7 2.5 6.3 2.5 13.5 0 10.8-6.5 20.3T594-277L167-126q-11 4-20.53.97-9.54-3.04-16.47-9.97-6.93-6.93-9.97-16.47Q117-161 121-172Zm791-506q-8 8-17 8t-17-8l-17-17q-13.8-14-34.5-14T792-695L589-491q-7.36 7-17.18 7T555-491q-7-7-7-16.82t7-17.18l203-204q28.07-28 67.53-28Q865-757 894-729l18 18q7 7.09 7 16.55 0 9.45-7 16.45ZM418-797q7.36-7 17.18-7t16.82 7l17 17q27 28.53 27 67.77Q496-673 469-644l-17 17q-7.36 7-17.18 7T418-627q-7-7-7-16.82t7-17.18l17-17q14-13.79 14-33.5T435-745l-18-18q-7-7.09-7-16.55 0-9.45 8-17.45Zm119-50q7-7 16.82-7t17.18 7l51 51q29 29 29 68.47 0 39.46-29 67.53L521-559q-7.09 7-16.55 7-9.45 0-17.45-7t-8-17.18q0-10.18 8-17.82l101-101q14-13.6 14-34t-14-34l-51-50q-7-7.36-7-17.18t7-16.82Zm340 407q-7 7-16.82 7T843-440l-51-51q-13.6-14-34-14t-34 14l-68 68q-7.36 7-17.18 7T622-423q-7-7-7-16.82t7-17.18l68-68q29-27 68.5-27.5T826-525l51 51q7 7.36 7 17.18T877-440Z'/></svg>");
71
71
 
72
72
  color-scheme: light dark;
73
73
  }
@@ -22,6 +22,7 @@
22
22
  <% }); %>
23
23
  </div>
24
24
  <% } %>
25
+ <%= render('_footer.html') %>
25
26
  </main>
26
27
  <% if (page.tocHtml) { %>
27
28
  <nav class="toc"><%= page.tocHtml %></nav>
@@ -4,6 +4,7 @@
4
4
  <%= render('_heading.html') %>
5
5
  <main class="body" data-pagefind-body>
6
6
  <%= content %>
7
+ <%= render('_footer.html') %>
7
8
  </main>
8
9
  <% if (page.toc && page.tocHtml) { %>
9
10
  <nav class="toc"><%= page.tocHtml %></nav>
@@ -7,6 +7,7 @@
7
7
  <code><%= page.downloadName %></code><%= render('_download.html') %>
8
8
  </div>
9
9
  <%= content %>
10
+ <%= render('_footer.html') %>
10
11
  </main>
11
12
  <% if (page.toc && page.tocHtml) { %>
12
13
  <nav class="toc"><%= page.tocHtml %></nav>