We code better now

This commit is contained in:
2022-06-12 14:22:15 +02:00
parent 929519225a
commit 0a0020b5c0
12 changed files with 230 additions and 166 deletions

View File

@ -1,3 +1,4 @@
import type { Diary, Project } from "../content/types";
import type { Command, Flag } from "./types";
function getCommandByName(name: string): Command|undefined {
@ -146,9 +147,16 @@ const project: Command = {
execute: (flags, args, _raw, cmdIf) => {
if (project.flags && checkFlagInclude(flags, project.flags.list)) {
const result = ["Found the following projects:"];
cmdIf.projects.projects.forEach(project => result.push(`\t${project.name}\t${project.short_desc}`));
const projects = cmdIf.content.filter(p => p.type === "project");
if(projects.length === 0) result.push("\tNo projects found.");
else projects.forEach(project => result.push(`\t${project.name}\t${project.short_desc}`));
result.push("And the following diaries:");
cmdIf.projects.diaries.forEach(diary => result.push(`\t${diary.name}\t${diary.short_desc}`));
const diaries = cmdIf.content.filter(p => p.type === "diary");
if(diaries.length === 0) result.push("\tNo diaries found.");
else diaries.forEach(diary => result.push(`\t${diary.name}\t${diary.short_desc}`));
return result;
}
@ -156,8 +164,7 @@ const project: Command = {
if (args[0] === "this") args[0] = "homepage";
let [pjt, ptype] = [cmdIf.projects.projects.find(p => p.name === args[0]), "project"];
if (!pjt) [pjt, ptype] = [cmdIf.projects.diaries.find(p => p.name === args[0]), "diary"];
let [pjt] = [cmdIf.content.find(p => p.name === args[0]) as Project|Diary|undefined];
if (!pjt) return [
`Cannot find project ${args[0]}!`,
"You can see available projects using 'project -l'."
@ -173,9 +180,8 @@ const project: Command = {
}
if (project.flags && checkFlagInclude(flags, project.flags.minimal)) return pjt.desc;
cmdIf.callbacks.setModalProjectType(ptype);
cmdIf.callbacks.setModalProject(pjt);
cmdIf.callbacks.setModalVisible(true);
if (cmdIf.callbacks?.setModalContent) cmdIf.callbacks.setModalContent(pjt);
if (cmdIf.callbacks?.setModalVisible) cmdIf.callbacks.setModalVisible(true);
return [];
}
};
@ -203,4 +209,4 @@ const clear: Command = {
execute: () => []
};
export const commandList = [about, help, man, project, exitCmd, clear];
export const commandList = [about, help, man, project, exitCmd, clear].sort((a, b) => a.name.localeCompare(b.name));

View File

@ -1,19 +1,18 @@
import type { ProjectList } from "../projects/types";
import type { ContentList } from "../content/types";
import { printSyntax, commandList } from "./definitions";
interface CommandInterfaceCallbacks {
setModalVisible: CallableFunction;
setModalProject: CallableFunction;
setModalProjectType: CallableFunction;
setModalVisible?: CallableFunction;
setModalContent?: CallableFunction;
}
export class CommandInterface {
callbacks: CommandInterfaceCallbacks;
projects: ProjectList;
callbacks?: CommandInterfaceCallbacks;
content: ContentList = [];
constructor(callbacks: CommandInterfaceCallbacks, projects: ProjectList) {
constructor(callbacks?: CommandInterfaceCallbacks, content?: ContentList) {
this.callbacks = callbacks;
this.projects = projects;
this.content = content || [];
}
static commandCompletion(input: string): string[] {

44
lib/content/generate.ts Normal file
View File

@ -0,0 +1,44 @@
import type { Project, Diary } from "./types";
import asciidoctor from "asciidoctor";
export const projectEmpty = "<div>Kein Projekt ausgewählt.</div>";
const projectNotFoundHtml = `<div class="${"error"}">Sorry! There is no data for this project. Please check back later to see if that changed!</div>`;
const projectServerErrorHtml = `<div class="${"error"}">Sorry! A server error happend when the project data was fetched!</div>`;
const ad = asciidoctor();
export async function generateContent(content: Project|Diary, selectedPage?: number): Promise<string> {
if(!content) return projectEmpty;
switch (content.type) {
case "project": return await generateProjectHTML(content);
case "diary": return await generateDiaryHTML(content, selectedPage);
default: return projectNotFoundHtml;
}
}
async function generateProjectHTML(project: Project): Promise<string> {
const resp = await fetch(`/content/projects/${project.name}.adoc`);
if (resp.status !== 200) return projectServerErrorHtml;
const adDoc = ad.load(await resp.text(), { attributes: { showtitle: true } });
return `${adDoc.convert(adDoc).toString()}
<hr>
<div id="footer">
<div id="footer-text">
Last updated: ${adDoc.getAttribute("docdatetime")} | <a href="https://git.c0ntroller.de/c0ntroller/frontpage-content/src/branch/senpai/projects/${project.name}.adoc">Document source</a>
</div>
</div>`;
}
async function generateDiaryHTML(diary: Diary, selectedPage?: number): Promise<string> {
const resp = selectedPage === undefined ? await fetch(`/content/diaries/${diary.name}.adoc`) : await fetch(`/content/diaries/${diary.name}/${diary.entries[selectedPage].filename}.adoc`);
if (resp.status !== 200) return projectServerErrorHtml;
const adDoc = ad.load(await resp.text(), { attributes: { showtitle: true } });
const gitfile = selectedPage === undefined ? `${diary.name}.adoc` : `${diary.name}/${diary.entries[selectedPage].filename}.adoc`;
return `${adDoc.convert(adDoc).toString()}
<hr>
<div id="footer">
<div id="footer-text">
Last updated: ${adDoc.getAttribute("docdatetime")} | <a href="https://git.c0ntroller.de/c0ntroller/frontpage-content/src/branch/senpai/diaries/${gitfile}">Document source</a>
</div>
</div>`;
}

View File

@ -1,9 +1,9 @@
export interface ProjectList {
projects: Project[];
diaries: Diary[];
}
export type ContentList = (Project|Diary)[];
export type ContentType = "project" | "diary";
export interface Project {
type: "project";
name: string;
desc: string[];
short_desc: string;
@ -12,6 +12,7 @@ export interface Project {
}
export interface Diary {
type: "diary";
name: string;
desc: string[];
short_desc: string;
@ -19,6 +20,6 @@ export interface Diary {
repo?: string;
entries: {
title: string;
path: string;
filename: string;
}[];
}