Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
144 changes: 144 additions & 0 deletions .github/readme-generate-en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
const { readdir, writeFile, stat } = require('fs/promises');
const fs = require('fs').promises;
const path = require('path');

const EN_ROOT = './en';
const README_PATH = './en/README.md';
const ignorePaths = ['README.md', 'template'];

const categories = {
vegetable_dish: {
title: 'Vegetable Dishes',
readme: '',
},
meat_dish: {
title: 'Meat Dishes',
readme: '',
},
aquatic: {
title: 'Seafood',
readme: '',
},
breakfast: {
title: 'Breakfast',
readme: '',
},
staple: {
title: 'Staples',
readme: '',
},
'semi-finished': {
title: 'Semi-finished Foods',
readme: '',
},
soup: {
title: 'Soups and Congee',
readme: '',
},
drink: {
title: 'Drinks',
readme: '',
},
condiment: {
title: 'Sauces and Other Ingredients',
readme: '',
},
dessert: {
title: 'Desserts',
readme: '',
},
};

async function main() {
try {
let README_BEFORE = '', README_MAIN = '', README_AFTER = '';
const markdownObj = await getAllMarkdown(EN_ROOT);

for (const markdown of markdownObj) {
// Paths in en/README.md are relative to en/.
const relPath = path.relative(EN_ROOT, markdown.path).split(path.sep).join('/');

if (relPath.startsWith('tips/advanced')) {
README_AFTER += inlineReadmeTemplate(markdown.title, relPath, markdown.file);
continue;
}

if (relPath.startsWith('tips')) {
README_BEFORE += inlineReadmeTemplate(markdown.title, relPath, markdown.file);
continue;
}

for (const category of Object.keys(categories)) {
if (relPath !== `dishes/${category}`) continue;
categories[category].readme += inlineReadmeTemplate(markdown.title, relPath, markdown.file);
}
}

for (const category of Object.values(categories)) {
README_MAIN += categoryReadmeTemplate(category.title, category.readme);
}

let README_TEMPLATE;

try {
README_TEMPLATE = await fs.readFile('./.github/templates/readme_template_en.md', 'utf-8');
} catch (error) {
README_TEMPLATE = `# How To Cook\n\n{{before}}\n\n{{main}}\n\n{{after}}`;
console.warn('readme_template_en.md not found, using default template');
}

await writeFile(
README_PATH,
README_TEMPLATE
.replace('{{before}}', README_BEFORE.trim())
.replace('{{main}}', README_MAIN.trim())
.replace('{{after}}', README_AFTER.trim()),
);
} catch (error) {
console.error(error);
}
}

// Collects every markdown file under dir, with its title taken from the H1.
// Files are sorted by title within each folder.
async function getAllMarkdown(dir) {
const files = [];
const subDirs = [];

for (const file of await readdir(dir)) {
if (ignorePaths.includes(file)) continue;
const filePath = path.join(dir, file);
const fileStat = await stat(filePath);
if (fileStat.isFile() && file.endsWith('.md')) {
const content = await fs.readFile(filePath, 'utf-8');
files.push({ path: dir, file, title: getTitle(content, file) });
} else if (fileStat.isDirectory()) {
subDirs.push(filePath);
}
}

files.sort((a, b) => a.title.localeCompare(b.title, 'en'));
subDirs.sort((a, b) => a.localeCompare(b, 'en'));

const paths = [...files];
for (const subDir of subDirs) {
paths.push(...await getAllMarkdown(subDir));
}
return paths;
}

function getTitle(content, file) {
const match = content.match(/^#\s+(.+)$/m);
if (!match) return file.replace('.md', '');
return match[1].trim().replace(/^How to Make /, '');
}

function inlineReadmeTemplate(title, dir, file) {
return `- [${title}](./${dir}/${file})\n`;
}

function categoryReadmeTemplate(title, inlineStr) {
return `\n### ${title}\n\n${inlineStr}`;
}

main();
2 changes: 2 additions & 0 deletions .github/templates/readme_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
[![Website](https://img.shields.io/website?url=https%3A%2F%2Fhowtocook.aiursoft.com)](https://howtocook.aiursoft.com)
[![Docker](https://img.shields.io/docker/pulls/aiursoft/howtocookviewer.svg)](https://hub.docker.com/r/aiursoft/howtocookviewer)

中文 | [English](./en/README.md)

最近宅在家做饭,作为程序员,我偶尔在网上找找菜谱和做法。但是这些菜谱往往写法千奇百怪,经常中间莫名出来一些材料。对于习惯了形式语言的程序员来说极其不友好。

所以,我计划自己搜寻菜谱并结合实际做菜的经验,准备用更清晰精准的描述来整理常见菜的做法,以方便程序员在家做饭。
Expand Down
38 changes: 38 additions & 0 deletions .github/templates/readme_template_en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# How To Cook: A Programmer's Guide to Cooking at Home

[![License](https://img.shields.io/github/license/Anduin2017/HowToCook)](../LICENSE)
[![GitHub contributors](https://img.shields.io/github/contributors/Anduin2017/HowToCook)](https://github.com/Anduin2017/HowToCook/graphs/contributors)

[中文](../README.md) | English

Lately I've been cooking at home a lot, and as a programmer I sometimes look up recipes online. But those recipes are written in all sorts of strange ways, with ingredients often appearing out of nowhere halfway through. That's extremely unfriendly to programmers who are used to formal languages.

So I plan to collect recipes myself and, combined with my actual cooking experience, organize how to make common dishes with clearer and more precise descriptions, to make it easier for programmers to cook at home.

I also hope this will be a community-driven, community-maintained open source project, so that more people can build an interesting repository together. Contributions are very welcome!

## About This Translation

This is an English translation of the original Chinese recipes. The Chinese version is the source of truth: if a translation looks wrong or out of date, compare it with the original and open an Issue or Pull request.

Every page maps to its Chinese original in [translation-map.json](./translation-map.json). Pictures are shared with the Chinese recipes rather than copied.

## How to Contribute

If you find a problem, just fix it and submit a Pull request.

When writing a new English recipe, please copy and edit the recipe template: [Sample Dish](./dishes/template/sample-dish.md).

## Getting Set Up

{{before}}

## Recipes

{{main}}

## Advanced Knowledge

If you've already made many of the dishes above, have learned the basics, and want to learn more advanced cooking techniques, read on:

{{after}}
1 change: 1 addition & 0 deletions .textlintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
en/**
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
[![Website](https://img.shields.io/website?url=https%3A%2F%2Fhowtocook.aiursoft.com)](https://howtocook.aiursoft.com)
[![Docker](https://img.shields.io/docker/pulls/aiursoft/howtocookviewer.svg)](https://hub.docker.com/r/aiursoft/howtocookviewer)

中文 | [English](./en/README.md)

最近宅在家做饭,作为程序员,我偶尔在网上找找菜谱和做法。但是这些菜谱往往写法千奇百怪,经常中间莫名出来一些材料。对于习惯了形式语言的程序员来说极其不友好。

所以,我计划自己搜寻菜谱并结合实际做菜的经验,准备用更清晰精准的描述来整理常见菜的做法,以方便程序员在家做饭。
Expand Down
Loading
Loading