Skip to content

Commit

Permalink
Remove obsolete menu generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ggodlewski committed Apr 17, 2024
1 parent 076bc56 commit 3c76290
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 82 deletions.
59 changes: 59 additions & 0 deletions apps/wgd-action-runner/site/navigation2menu.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/node

/**
* In order to generate menu.en.json from markdown file run:
*
* cat content/navigation.md | ./navigation2menu.js > config/_default/menu.en.json
*/

// Work on POSIX and Windows
import fs from 'node:fs';
const stdinBuffer = fs.readFileSync(0); // STDIN_FILENO = 0

const markdown = stdinBuffer.toString();

let weight = 10;

const parentStack = [];
const menu = [];

let lastContent = 'First line';

for (const line of markdown.split('\n')) {
if (!line.match(/^ *\* /)) {
continue;
}
const indentPart = line.replace(/(^ *\* ).*/, '$1');
const markdownLink = line.substring(indentPart.length);
const matched = markdownLink.match(/\[([^\]]+)]\(([^)]+)\)/);
if (!matched) {
console.warn(`Warning: navigation.md menu has "${markdownLink}" without url near: "${lastContent}"`);
continue;
}
const [_, name, pageRef] = matched;
const level = (indentPart.length - 2)/3;

while (parentStack.length > level) {
parentStack.pop();
}

const identifier = pageRef;

if (pageRef.startsWith('http://') || pageRef.startsWith('https://') || fs.existsSync('./content/' + pageRef)) {
menu.push({
identifier,
name,
pageRef,
parent: parentStack[parentStack.length - 1],
weight
});
} else {
console.warn(`Warning: navigation.md menu has "${markdownLink}" without file: "${pageRef}"`);
}

weight += 10;
parentStack.push(identifier);
lastContent = markdownLink;
}

console.log(JSON.stringify({ main: menu }, null, 4));
97 changes: 27 additions & 70 deletions src/containers/transform/TransformContainer.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
import winston from 'winston';
import {Container, ContainerConfig, ContainerConfigArr, ContainerEngine} from '../../ContainerEngine';
import {FileContentService} from '../../utils/FileContentService';
import {appendConflict, DirectoryScanner, stripConflict} from './DirectoryScanner';
import {GoogleFilesScanner} from './GoogleFilesScanner';
import {convertToRelativeMarkDownPath, convertToRelativeSvgPath} from '../../LinkTranslator';
import {LocalFilesGenerator} from './LocalFilesGenerator';
import {QueueTransformer} from './QueueTransformer';
import {generateNavigationHierarchy, NavigationHierarchy} from './generateNavigationHierarchy';
import {ConflictFile, LocalFile, LocalFileMap, RedirFile} from '../../model/LocalFile';
import {TaskLocalFileTransform} from './TaskLocalFileTransform';
import {GoogleFile, MimeTypes} from '../../model/GoogleFile';
import {generateDirectoryYaml, parseDirectoryYaml} from './frontmatters/generateDirectoryYaml';
import {getContentFileService, removeMarkDownsAndImages} from './utils';
import {LocalLog} from './LocalLog';
import {LocalLinks} from './LocalLinks';
import {OdtProcessor} from '../../odt/OdtProcessor';
import {UnMarshaller} from '../../odt/UnMarshaller';
import {DocumentContent, LIBREOFFICE_CLASSES} from '../../odt/LibreOffice';
import {TaskRedirFileTransform} from './TaskRedirFileTransform';
import {TocGenerator} from './frontmatters/TocGenerator';
import {FileId} from '../../model/model';
import {fileURLToPath} from 'url';
import {MarkdownTreeProcessor} from './MarkdownTreeProcessor';
import {LunrIndexer} from '../search/LunrIndexer';
import {JobManagerContainer} from '../job/JobManagerContainer';
import {UserConfigService} from '../google_folder/UserConfigService';
import winston from 'winston';
import Transport from 'winston-transport';

import {FileId} from '../../model/model.ts';
import {MimeTypes} from '../../model/GoogleFile.ts';
import {ConflictFile, LocalFile, RedirFile} from '../../model/LocalFile.ts';
import {Container, ContainerConfig, ContainerConfigArr, ContainerEngine} from '../../ContainerEngine.ts';
import {FileContentService} from '../../utils/FileContentService.ts';
import {convertToRelativeMarkDownPath, convertToRelativeSvgPath} from '../../LinkTranslator.ts';
import {JobManagerContainer} from '../job/JobManagerContainer.ts';
import {UserConfigService} from '../google_folder/UserConfigService.ts';
import {LunrIndexer} from '../search/LunrIndexer.ts';
import {appendConflict, DirectoryScanner, stripConflict} from './DirectoryScanner.ts';
import {GoogleFilesScanner} from './GoogleFilesScanner.ts';
import {LocalFilesGenerator} from './LocalFilesGenerator.ts';
import {QueueTransformer} from './QueueTransformer.ts';
import {TaskLocalFileTransform} from './TaskLocalFileTransform.ts';
import {generateDirectoryYaml, parseDirectoryYaml} from './frontmatters/generateDirectoryYaml.ts';
import {getContentFileService, removeMarkDownsAndImages} from './utils.ts';
import {LocalLog} from './LocalLog.ts';
import {LocalLinks} from './LocalLinks.ts';
import {TaskRedirFileTransform} from './TaskRedirFileTransform.ts';
import {TocGenerator} from './frontmatters/TocGenerator.ts';
import {MarkdownTreeProcessor} from './MarkdownTreeProcessor.ts';

const __filename = fileURLToPath(import.meta.url);

function doesExistIn(googleFolderFiles: LocalFile[], localFile: LocalFile) {
Expand Down Expand Up @@ -177,7 +174,7 @@ export class TransformLog extends Transport {
super(options);
}

log(info, callback) {
log(info: { level: string, errorMdFile: string, errorMdMsg: string }, next: () => void) {
switch (info.level) {
case 'error':
case 'warn':
Expand All @@ -191,16 +188,15 @@ export class TransformLog extends Transport {
}
}

if (callback) {
callback(null, true);
if (next) {
next();
}
}
}

export class TransformContainer extends Container {
private logger: winston.Logger;
private generatedFileService: FileContentService;
private hierarchy: NavigationHierarchy = {};
private localLog: LocalLog;
private localLinks: LocalLinks;
private filterFilesIds: FileId[];
Expand Down Expand Up @@ -286,7 +282,7 @@ export class TransformContainer extends Container {
continue;
}

const jobManagerContainer = <JobManagerContainer>this.engine.getContainer('job_manager');
const jobManagerContainer = <JobManagerContainer><unknown>this.engine.getContainer('job_manager');

const task = new TaskLocalFileTransform(
this.logger,
Expand Down Expand Up @@ -404,16 +400,6 @@ export class TransformContainer extends Container {
await markdownTreeProcessor.regenerateTree(rootFolderId);
await markdownTreeProcessor.save();

this.hierarchy = await this.loadNavigationHierarchy();
for (const k in this.hierarchy) {
const item = this.hierarchy[k];
if (item.identifier) {
const [, path] = await markdownTreeProcessor.findById(item.identifier);
item.pageRef = path;
}
}
await this.writeHugoMenu(this.hierarchy);

const indexer = new LunrIndexer();
await markdownTreeProcessor.walkTree((page) => {
indexer.addPage(page);
Expand Down Expand Up @@ -526,35 +512,6 @@ export class TransformContainer extends Container {
async destroy(): Promise<void> {
}

async writeHugoMenu(hierarchy: NavigationHierarchy) {
const menus = {
main: Object.values(hierarchy)
};

await this.generatedFileService.mkdir('config/_default');
await this.generatedFileService.writeJson('config/_default/menu.en.json', menus);
}

async loadNavigationHierarchy(): Promise<NavigationHierarchy> {
const googleFiles: GoogleFile[] = await this.filesService.readJson('.folder-files.json') || [];

const navigationFile = googleFiles.find(googleFile => googleFile.name === '.navigation' || googleFile.name === 'navigation');
if (navigationFile) {
const odtPath = this.filesService.getRealPath() + '/' + navigationFile.id + '.odt';
const processor = new OdtProcessor(odtPath);
await processor.load();
const content = processor.getContentXml();
const parser = new UnMarshaller(LIBREOFFICE_CLASSES, 'DocumentContent');
const navDoc: DocumentContent = parser.unmarshal(content);

if (navDoc) {
return await generateNavigationHierarchy(navDoc, this.logger);
}
}

return {};
}

onProgressNotify(callback: ({total, completed, warnings, failed}: { total?: number; completed?: number, warnings?: number, failed?: number }) => void) {
this.progressNotifyCallback = callback;
}
Expand Down
24 changes: 12 additions & 12 deletions src/containers/transform/generateNavigationHierarchy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

import {urlToFolderId} from '../../utils/idParsers';
import {FileId} from '../../model/model';
import {DocumentContent, TextLink, TextList, TextParagraph, TextSpace, TextSpan, TextTab} from '../../odt/LibreOffice';
// Legacy menu generator, use: navigation2menu.mjs

import {urlToFolderId} from '../../utils/idParsers.ts';
import {FileId} from '../../model/model.ts';
import {DocumentContent, TextLink, TextList, TextParagraph, TextSpace, TextSpan, TextTab} from '../../odt/LibreOffice.ts';

export interface NavigationHierarchyNode {
name: string;
Expand Down Expand Up @@ -107,17 +109,15 @@ function processPara(para: TextParagraph, ctx: NavigationProcessContext, level:
function processList(textList: TextList, ctx: NavigationProcessContext, level = 0) {
for (const textListItem of textList.list) {
for (const paraOrList of textListItem.list) {
if (paraOrList.type === 'list') {
processList(<TextList>paraOrList, ctx, level + 1);
continue;
}

if (paraOrList.type === 'paragraph') {
processPara(<TextParagraph>paraOrList, ctx, level);
continue;
switch (paraOrList.type) {
case 'list':
processList(<TextList>paraOrList, ctx, level + 1);
break;
case 'paragraph':
processPara(<TextParagraph>paraOrList, ctx, level);
break;
}
}

}
}

Expand Down

0 comments on commit 3c76290

Please sign in to comment.