Add documentation

This commit is contained in:
Oystein Kristoffer Tveit 2020-05-25 23:03:19 +02:00
parent ab4bdcbdc8
commit df9a4d5138
4 changed files with 47 additions and 19 deletions

View File

@ -1,6 +1,6 @@
const fs = require('fs'); const fs = require('fs');
/* Local files */ /* Import local files */
const {fetchKanjiFromTxt, fetchKanjiFromJisho} = require('./src/dataFetching.js'); const {fetchKanjiFromTxt, fetchKanjiFromJisho} = require('./src/dataFetching.js');
const {getKanjiTexData} = require('./src/texConversion.js'); const {getKanjiTexData} = require('./src/texConversion.js');
const {kanjiTable} = require('./src/kanjiTables.js'); const {kanjiTable} = require('./src/kanjiTables.js');
@ -13,11 +13,11 @@ async function main(jlptLevel) {
const kanjiArray = await fetchKanjiFromTxt(`./data/txt/${jlptLevel}.txt`); const kanjiArray = await fetchKanjiFromTxt(`./data/txt/${jlptLevel}.txt`);
console.log(`${jlptLevelCaps}: Fetched txt`); console.log(`${jlptLevelCaps}: Fetched txt`);
const results = await fetchKanjiFromJisho(kanjiArray); const jishoResults = await fetchKanjiFromJisho(kanjiArray);
console.log(`${jlptLevelCaps}: Fetched Jisho data`); console.log(`${jlptLevelCaps}: Fetched Jisho data`);
const sortedKanjiArray = results.map(result => result.query); const sortedKanjiArray = jishoResults.map(result => result.query);
const texData = getKanjiTexData(results); const texData = getKanjiTexData(jishoResults);
console.log(`${jlptLevelCaps}: Processed pages`); console.log(`${jlptLevelCaps}: Processed pages`);
const resultTable = kanjiTable(sortedKanjiArray); const resultTable = kanjiTable(sortedKanjiArray);
@ -37,7 +37,8 @@ async function main(jlptLevel) {
fs.writeFile(`./data/pages/${jlptLevel}.tex`, resultPage, (err) => {if (err) console.error(err)}); fs.writeFile(`./data/pages/${jlptLevel}.tex`, resultPage, (err) => {if (err) console.error(err)});
} }
async function mainWrapper() { /* Handle args */
async function argWrapper() {
try { try {
if (!/n\d/.test(process.argv[2])) throw 'Input not valid'; if (!/n\d/.test(process.argv[2])) throw 'Input not valid';
@ -47,4 +48,4 @@ async function mainWrapper() {
} }
} }
mainWrapper(); argWrapper();

View File

@ -3,12 +3,23 @@ const util = require('util');
const jishoApi = require('unofficial-jisho-api'); const jishoApi = require('unofficial-jisho-api');
const jisho = new jishoApi(); const jisho = new jishoApi();
/**
* Reads a txt file and splits the characters into an array
* @param {string} file Path to file
* @returns {string[]} Kanji
*/
async function fetchKanjiFromTxt(file) { async function fetchKanjiFromTxt(file) {
const read = util.promisify(fs.readFile); const read = util.promisify(fs.readFile);
const data = await read(file, 'utf8'); const data = await read(file, 'utf8');
return data.split(''); return data.split('');
} }
/**
* Makes a delayed kanji search request in order not to overload the server.
* @param {string} kanji The character to search for
* @param {number} delay Number of milliseconds delay to the request
* @return {promise} A promise that's going to run a request after the specified delay
*/
async function delayedJishoCall(kanji, delay) { async function delayedJishoCall(kanji, delay) {
return new Promise((res, rej) => { return new Promise((res, rej) => {
setTimeout(() => { setTimeout(() => {
@ -20,7 +31,11 @@ async function delayedJishoCall(kanji, delay) {
/* Sort list of result array */ /* Sort list of result array */
const sortKanji = (kanjiData) => kanjiData.sort((a, b) => (a.strokeCount > b.strokeCount) ? 1 : -1); const sortKanji = (kanjiData) => kanjiData.sort((a, b) => (a.strokeCount > b.strokeCount) ? 1 : -1);
/* Function to fetch jisho data for all kanjis in an array */ /**
* Searches for kanji with a 50ms interval between each request.
* @param {string[]} kanjiArray A list of kanji to search for.
* @returns {object} JSON data containing a sorted list of search responses.
*/
async function fetchKanjiFromJisho(kanjiArray) { async function fetchKanjiFromJisho(kanjiArray) {
const promises = kanjiArray.map(async (kanji, i) => await delayedJishoCall(kanji, i*50)); const promises = kanjiArray.map(async (kanji, i) => await delayedJishoCall(kanji, i*50));
const data = await Promise.all(promises); const data = await Promise.all(promises);

View File

@ -5,7 +5,9 @@
\definecolor{myGreen}{RGB}{72, 194, 78} \definecolor{myGreen}{RGB}{72, 194, 78}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ---------------------------------------------------------------------------- %
% JLPT Section %
% ---------------------------------------------------------------------------- %
\newcommand{\jlptSection}[1]{ \newcommand{\jlptSection}[1]{
\section*{\uppercase{#1}} \section*{\uppercase{#1}}
@ -19,7 +21,9 @@
\break \break
} }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ---------------------------------------------------------------------------- %
% Header line %
% ---------------------------------------------------------------------------- %
\newcommand{\taughtIn}[1]{ \newcommand{\taughtIn}[1]{
Taught in: #1 \newline Taught in: #1 \newline
@ -76,7 +80,9 @@
\vspace{1cm} \vspace{1cm}
} }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ---------------------------------------------------------------------------- %
% Meaning %
% ---------------------------------------------------------------------------- %
\newtcolorbox{meaningBox}{ \newtcolorbox{meaningBox}{
enhanced, enhanced,
@ -98,7 +104,9 @@
\end{center} \end{center}
} }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ---------------------------------------------------------------------------- %
% Kunyomi and Onyomi %
% ---------------------------------------------------------------------------- %
\newtcolorbox{kunyomiBox}{ \newtcolorbox{kunyomiBox}{
enhanced, enhanced,
@ -143,7 +151,9 @@
\vspace{0.5cm} \vspace{0.5cm}
} }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ---------------------------------------------------------------------------- %
% Kanji Drawing Box %
% ---------------------------------------------------------------------------- %
\newCJKfontfamily\drawingKanji{Choumei} \newCJKfontfamily\drawingKanji{Choumei}
\newCJKfontfamily\drawingFirstKanji{KanjiStrokeOrders} \newCJKfontfamily\drawingFirstKanji{KanjiStrokeOrders}

View File

@ -1,7 +1,6 @@
% !TEX program = xelatex % !TEX program = xelatex
\documentclass{article} \documentclass{article}
\usepackage{fontspec}
\usepackage{hyperref} \usepackage{hyperref}
\hypersetup{ \hypersetup{
colorlinks=true, colorlinks=true,
@ -18,18 +17,21 @@
top=20mm, top=20mm,
} }
\setlength{\parskip}{0.5em}
\setlength{\parindent}{0pt}
\usepackage{longtable} %N1 table is too long. \usepackage{longtable} %N1 table is too long.
%Japanese typesetting and fonts
\usepackage[japanese]{babel} \usepackage[japanese]{babel}
\usepackage{xeCJK} \usepackage{xeCJK}
\usepackage{fontspec}
\setCJKmainfont{Noto Sans Mono CJK JP} \setCJKmainfont{Noto Sans Mono CJK JP}
\setmainfont{Open Sans} \setmainfont{Open Sans}
\input{./kanjiLib.tex} \input{./kanjiLib.tex}
\setlength{\parindent}{0pt} \usepackage{etoc} %For local tocs containing level based kanji list.
\usepackage{etoc}
\begin{document} \begin{document}
@ -44,11 +46,11 @@
%TODO: Add more detailed information %TODO: Add more detailed information
Thanks to for making both the Choumei font and the variant containing stroke order numbers. You can find his work at \url{https://www.nihilist.org.uk/} Thanks to Timothy Eyre for making the Choumei font and the edition containing stroke order numbers. You can find his work at \url{https://www.nihilist.org.uk/}
Metadata is taken from \url{https://jisho.org/} Kanji data is taken from \url{https://jisho.org/}
This is a list of all kanji in N5 (according to \href{http://www.tanos.co.uk/jlpt/skills/kanji/}{tanos.co.uk}) This document splits the kanjis into JLPT levels. Please note that there is no official list of kanji JLPT levels. This list is based on \href{http://www.tanos.co.uk/jlpt/skills/kanji/}{tanos.co.uk}.
\break \break