Get includes working relatively.

This commit is contained in:
Joshua Bemenderfer 2023-03-06 22:22:48 -05:00
parent f42225bd13
commit 79d044ebac
9 changed files with 68 additions and 28 deletions

1
docs/pages/docs/c.tce Normal file
View File

@ -0,0 +1 @@
Include ../../../packages/c/docs/index.tce

View File

@ -0,0 +1 @@
Include ../../../packages/js/docs/index.tce

View File

@ -2,13 +2,16 @@ import knownNodes from './nodes/index.js'
import { useDocument } from '@terrace-lang/js'
import { createFileReader } from '@terrace-lang/js/readers/node-readline'
export default async function(inputPath) {
const doc = useDocument(createFileReader(inputPath))
import process from 'node:process'
import path from 'node:path'
export default async function(filePath) {
filePath = path.resolve(filePath)
const doc = useDocument(createFileReader(filePath))
const { next, line, match, tail, level, head } = doc
const pageData = {
const page = {
type: `Page`,
path: inputPath,
title: '',
description: [],
layout: '',
@ -16,27 +19,37 @@ export default async function(inputPath) {
children: []
}
const context = {
page,
filePath
}
const originalCWD = process.cwd()
while(await next()) {
if (!line()) continue
if (match('title')) pageData.title = tail()
else if (match('layout')) pageData.layout = tail()
if (match('title')) page.title = tail()
else if (match('layout')) page.layout = tail()
else if (match('description')) {
const l = level()
while(await next(l)) {
pageData.description.push(line(l))
page.description.push(line(l))
}
}
else if (match('Section')) {
pageData.children.push(await knownNodes.Section(doc, level(), pageData))
page.children.push(await knownNodes.Section(doc, level(), context))
}
else if (match('Include')) {
page.children.push(await knownNodes.Include(doc, level(), context))
}
}
process.chdir(originalCWD)
// Structure headings into tree.
pageData.headings.forEach((heading, index) => {
page.headings.forEach((heading, index) => {
let parent = null
for (let i = index; i > 0; --i) {
if (pageData.headings[i].level === heading.level - 1) {
parent = pageData.headings[i]
if (page.headings[i].level === heading.level - 1) {
parent = page.headings[i]
break
}
}
@ -44,7 +57,7 @@ export default async function(inputPath) {
if (parent) parent.children.push(heading)
})
pageData.headings = pageData.headings.filter(h => h.level === 2)
page.headings = page.headings.filter(h => h.level === 2)
return pageData
return page
}

View File

@ -1,6 +1,6 @@
import slugify from '@sindresorhus/slugify'
export default async function (doc, rootLevel, pageData) {
export default async function (doc, rootLevel, context) {
const { next, line, match, tail, level, head } = doc
const headingLevel = +tail().split(' ')[0]
@ -22,7 +22,7 @@ export default async function (doc, rootLevel, pageData) {
if (match('href')) node.href = tail()
}
pageData.headings.push(node)
context.page.headings.push(node)
return node
}

View File

@ -2,10 +2,12 @@ import { useDocument } from '@terrace-lang/js/document'
import { createFileReader } from '@terrace-lang/js/readers/node-readline'
import fs from 'fs/promises'
import path from 'path'
import process from 'node:process'
import knownNodes from './index.js'
export default async function (originalDoc, rootLevel, ...args) {
const includedDoc = useDocument(createFileReader(originalDoc.tail()))
export default async function (originalDoc, rootLevel, context) {
const includePath = originalDoc.tail()
const includedDoc = useDocument(createFileReader(includePath))
const { next, head, tail, level } = includedDoc
const node = {
@ -14,13 +16,21 @@ export default async function (originalDoc, rootLevel, ...args) {
children: []
}
const root = path.dirname(context.filePath)
const originalFilepath = context.filePath
context.filePath = includePath
process.chdir(path.dirname(originalFilepath))
while (await next()) {
if (!head()) continue
const block = head()
if (!knownNodes[block]) continue
node.children.push(await knownNodes[block](includedDoc, level(), ...args))
node.children.push(await knownNodes[block](includedDoc, level(), context))
}
process.chdir(path.dirname(originalFilepath))
return node
}

View File

@ -9,7 +9,7 @@
<meta name="description" content="{{ page.description | join(' ') }}"/>
<link rel="stylesheet" href="/main.css"/>
<link rel="stylesheet" href="/public/styles/highlightjs-theme.css"/>
{{ googleFonts('https://fonts.googleapis.com/css2?family=Fredoka:wght@300;400;500&display=swap') }}
{{ 'https://fonts.googleapis.com/css2?family=Fredoka:wght@300;400;500&display=swap' | googleFonts }}
</head>
<body class="text-base pt-16">
{{ Node('Navbar', {}, { headings: page.headings, url: url }) }}

View File

@ -8,7 +8,9 @@ import featherIcons from './util/feather-icons.js'
const pages = {
'/': './pages/index.tce',
'/about/': './pages/about.tce'
'/about/': './pages/about.tce',
'/docs/javascript/': './pages/docs/javascript.tce',
'/docs/c/': './pages/docs/c.tce'
}
async function render() {
@ -17,7 +19,14 @@ async function render() {
for (const [urlPath, filePath] of Object.entries(pages)) {
const env = nunjucks.configure('renderer/', { autoescape: false })
env.addGlobal('googleFonts', googleFonts)
env.addFilter('googleFonts', async (val, cb) => {
try {
cb(null, await googleFonts(val))
} catch (e) {
cb(e)
}
}, true)
env.addGlobal('featherIcons', featherIcons)
HighlightJS.registerLanguage('terrace', () => ({
@ -36,7 +45,13 @@ async function render() {
})
const page = await readPage(filePath)
const result = env.render('layout.njk', { page, url: urlPath, file: filePath })
const result = await new Promise((resolve, reject) => {
env.render('layout.njk', { page, url: urlPath, file: filePath }, (err, res) => {
if (err) return reject(err)
resolve(res)
})
})
await fs.mkdir(path.join('dist', urlPath), { recursive: true })
await fs.writeFile(path.join('dist', urlPath, 'index.html'), result)

View File

@ -77,8 +77,8 @@ Section light
return 0;
}
Include ./src/docs/c/core-api.inc.tce
Include ./src/docs/c/document-api.inc.tce
Include ./core-api.inc.tce
Include ./document-api.inc.tce
Heading 2 Contributing
class mt-12

View File

@ -35,10 +35,10 @@ Section light
# Yarn (https://yarnpkg.com/)
$ yarn add @terrace-lang/js
Include ./src/docs/javascript/core-api.inc.tce
Include ./src/docs/javascript/document-api.inc.tce
Include ./src/docs/javascript/reader-api.inc.tce
Include ./src/docs/javascript/recipes.inc.tce
Include ./core-api.inc.tce
Include ./document-api.inc.tce
Include ./reader-api.inc.tce
Include ./recipes.inc.tce
Heading 2 Contributing
class mt-12