From 9b41023c43add194d69259e0a8a8c323aa44c778 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 6 Jul 2018 02:58:29 +0900 Subject: wip --- src/server/web/docs.ts | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'src/server/web/docs.ts') diff --git a/src/server/web/docs.ts b/src/server/web/docs.ts index e65cc87b12..849194388c 100644 --- a/src/server/web/docs.ts +++ b/src/server/web/docs.ts @@ -5,9 +5,53 @@ import ms = require('ms'); import * as Router from 'koa-router'; import * as send from 'koa-send'; +import { Context } from 'cafy'; +import ObjectContext from 'cafy/built/types/object'; +import config from '../../config'; +import generateVars from '../../client/docs/vars'; const docs = `${__dirname}/../../client/docs/`; +// WIP type +const parseEPDefParam = (key: string, param: Context) => { + return Object.assign({ + name: key, + type: param.getType() + }, param.data); +}; + +const sortParams = (params: Array<{name: string}>) => { + params.sort((a, b) => { + if (a.name < b.name) + return -1; + if (a.name > b.name) + return 1; + return 0; + }); + return params; +}; + +// WIP type +const extractDefs = (params: Context[]) => { + let defs: any[] = []; + + params.forEach(param => { + if (param.data && param.data.ref) { + const props = (param as ObjectContext).props; + defs.push({ + name: param.data.ref, + params: sortParams(Object.keys(props).map(k => parseEPDefParam(k, props[k]))) + }); + + const childDefs = extractDefs(Object.keys(props).map(k => props[k])); + + defs = defs.concat(childDefs); + } + }); + + return sortParams(defs); +}; + const router = new Router(); router.get('/assets/*', async ctx => { @@ -18,6 +62,32 @@ router.get('/assets/*', async ctx => { }); }); +router.get('/*/api/endpoints/*', async ctx => { + const ep = require('../../../built/server/api/endpoints/' + ctx.params[1]).meta; + + const vars = { + endpoint: ep.name, + url: { + host: config.api_url, + path: ep.name + }, + desc: ep.desc, + // @ts-ignore + params: sortParams(Object.keys(ep.params).map(k => parseEPDefParam(k, ep.params[k]))), + paramDefs: extractDefs(Object.keys(ep.params).map(k => ep.params[k])), + }; + console.log(vars); + + const commonVars = await generateVars(); + + await ctx.render('../../../../src/client/docs/api/endpoints/view', Object.assign({}, vars, { + lang: 'ja', + title: ep.name, + kebab: (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(), + common: commonVars + })); +}); + router.get('*', async ctx => { await send(ctx, `${ctx.params[0]}.html`, { root: docs -- cgit v1.2.3-freya From 029b92935c6119a01759c0e687eb0d9cef2dcf68 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 6 Jul 2018 12:17:38 +0900 Subject: wip --- gulpfile.ts | 2 +- locales/index.js | 27 +++++ locales/index.ts | 34 ------ src/build/i18n.ts | 10 +- src/client/docs/api/endpoints/view.pug | 4 +- src/client/docs/api/gulpfile.ts | 201 --------------------------------- src/client/docs/api/mixins.pug | 14 +-- src/client/docs/gulpfile.ts | 59 ---------- src/server/web/docs.ts | 8 +- webpack.config.ts | 2 +- 10 files changed, 49 insertions(+), 312 deletions(-) create mode 100644 locales/index.js delete mode 100644 locales/index.ts delete mode 100644 src/client/docs/api/gulpfile.ts (limited to 'src/server/web/docs.ts') diff --git a/gulpfile.ts b/gulpfile.ts index 49a80879d2..607f77fc31 100644 --- a/gulpfile.ts +++ b/gulpfile.ts @@ -20,7 +20,7 @@ import * as replace from 'gulp-replace'; import * as htmlmin from 'gulp-htmlmin'; const uglifyes = require('uglify-es'); -import locales from './locales'; +const locales = require('./locales'); import { fa } from './src/build/fa'; const client = require('./built/client/meta.json'); import config from './src/config'; diff --git a/locales/index.js b/locales/index.js new file mode 100644 index 0000000000..95cc339169 --- /dev/null +++ b/locales/index.js @@ -0,0 +1,27 @@ +/** + * Languages Loader + */ + +const fs = require('fs'); +const yaml = require('js-yaml'); + +const loadLang = lang => yaml.safeLoad( + fs.readFileSync(`./locales/${lang}.yml`, 'utf-8')); + +const native = loadLang('ja'); + +const langs = { + 'de': loadLang('de'), + 'en': loadLang('en'), + 'fr': loadLang('fr'), + 'ja': native, + 'pl': loadLang('pl'), + 'es': loadLang('es') +}; + +Object.entries(langs).map(([, locale]) => { + // Extend native language (Japanese) + locale = Object.assign({}, native, locale); +}); + +module.exports = langs; diff --git a/locales/index.ts b/locales/index.ts deleted file mode 100644 index 45b5df0957..0000000000 --- a/locales/index.ts +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Languages Loader - */ - -import * as fs from 'fs'; -import * as yaml from 'js-yaml'; - -export type LangKey = 'de' | 'en' | 'fr' | 'ja' | 'pl' | 'es'; -export type LocaleObject = { [key: string]: any }; - -const loadLang = (lang: LangKey) => yaml.safeLoad( - fs.readFileSync(`./locales/${lang}.yml`, 'utf-8')) as LocaleObject; - -const native = loadLang('ja'); - -const langs: { [key: string]: LocaleObject } = { - 'de': loadLang('de'), - 'en': loadLang('en'), - 'fr': loadLang('fr'), - 'ja': native, - 'pl': loadLang('pl'), - 'es': loadLang('es') -}; - -Object.entries(langs).map(([, locale]) => { - // Extend native language (Japanese) - locale = Object.assign({}, native, locale); -}); - -export function isAvailableLanguage(lang: string): lang is LangKey { - return lang in langs; -} - -export default langs; diff --git a/src/build/i18n.ts b/src/build/i18n.ts index dc48251c95..4ed3c7a2df 100644 --- a/src/build/i18n.ts +++ b/src/build/i18n.ts @@ -2,7 +2,7 @@ * Replace i18n texts */ -import locale, { isAvailableLanguage, LocaleObject } from '../../locales'; +const locale = require('../../locales'); export default class Replacer { private lang: string; @@ -16,8 +16,8 @@ export default class Replacer { this.replacement = this.replacement.bind(this); } - private get(path: string, key: string): string { - if (!isAvailableLanguage(this.lang)) { + public get(path: string, key: string): string { + if (!(this.lang in locale)) { console.warn(`lang '${this.lang}' is not supported`); return key; // Fallback } @@ -28,7 +28,7 @@ export default class Replacer { if (path) { if (text.hasOwnProperty(path)) { - text = text[path] as LocaleObject; + text = text[path]; } else { console.warn(`path '${path}' not found in '${this.lang}'`); return key; // Fallback @@ -38,7 +38,7 @@ export default class Replacer { // Check the key existance const error = key.split('.').some(k => { if (text.hasOwnProperty(k)) { - text = (text as LocaleObject)[k]; + text = text[k]; return false; } else { return true; diff --git a/src/client/docs/api/endpoints/view.pug b/src/client/docs/api/endpoints/view.pug index f8795c8442..24fff1b798 100644 --- a/src/client/docs/api/endpoints/view.pug +++ b/src/client/docs/api/endpoints/view.pug @@ -17,7 +17,7 @@ block main p#desc= desc[lang] || desc['ja'] section - h2 %i18n:docs.api.endpoints.params% + h2= i18n('docs.api.endpoints.params') +propTable(params) if paramDefs @@ -28,5 +28,5 @@ block main if res section - h2 %i18n:docs.api.endpoints.res% + h2= i18n('docs.api.endpoints.res') +propTable(res) diff --git a/src/client/docs/api/gulpfile.ts b/src/client/docs/api/gulpfile.ts deleted file mode 100644 index 4b38058b5c..0000000000 --- a/src/client/docs/api/gulpfile.ts +++ /dev/null @@ -1,201 +0,0 @@ -/** - * Gulp tasks - */ - -import * as fs from 'fs'; -import * as path from 'path'; -import * as glob from 'glob'; -import * as gulp from 'gulp'; -import * as pug from 'pug'; -import * as yaml from 'js-yaml'; -import * as mkdirp from 'mkdirp'; - -import locales from '../../../../locales'; -import I18nReplacer from '../../../build/i18n'; -import fa from '../../../build/fa'; -import config from './../../../config'; - -import generateVars from '../vars'; -import { Context } from 'cafy'; -import ObjectContext from 'cafy/built/types/object'; - -const langs = Object.keys(locales); - -const kebab = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(); - -const parseParam = (param: any) => { - const id = param.type.match(/^id\((.+?)\)|^id/); - const entity = param.type.match(/^entity\((.+?)\)/); - const isObject = /^object/.test(param.type); - const isDate = /^date/.test(param.type); - const isArray = /\[\]$/.test(param.type); - if (id) { - param.kind = 'id'; - param.type = 'string'; - param.entity = id[1]; - if (isArray) { - param.type += '[]'; - } - } - if (entity) { - param.kind = 'entity'; - param.type = 'object'; - param.entity = entity[1]; - if (isArray) { - param.type += '[]'; - } - } - if (isObject) { - param.kind = 'object'; - } - if (isDate) { - param.kind = 'date'; - param.type = 'string'; - if (isArray) { - param.type += '[]'; - } - } - - return param; -}; - -// WIP type -const parseEPDefParam = (key: string, param: Context) => { - return Object.assign({ - name: key, - type: param.getType() - }, param.data); -}; - -const sortParams = (params: Array<{name: string}>) => { - params.sort((a, b) => { - if (a.name < b.name) - return -1; - if (a.name > b.name) - return 1; - return 0; - }); - return params; -}; - -// WIP type -const extractDefs = (params: Context[]) => { - let defs: any[] = []; - - params.forEach(param => { - if (param.data && param.data.ref) { - const props = (param as ObjectContext).props; - defs.push({ - name: param.data.ref, - params: sortParams(Object.keys(props).map(k => parseEPDefParam(k, props[k]))) - }); - - const childDefs = extractDefs(Object.keys(props).map(k => props[k])); - - defs = defs.concat(childDefs); - } - }); - - return sortParams(defs); -}; - -gulp.task('doc:api', [ - 'doc:api:endpoints', - 'doc:api:entities' -]); - -gulp.task('doc:api:endpoints', ['build:ts'], async () => { - const commonVars = await generateVars(); - glob('./built/server/api/endpoints/**/*.js', (globErr, files) => { - if (globErr) { - console.error(globErr); - return; - } - console.log(files.map(file => require('../../../../' + file))); - - files.map(file => require('../../../../' + file)).filter(x => x.meta).map(x => x.meta).forEach(ep => { - console.log(ep); - const vars = { - endpoint: ep.name, - url: { - host: config.api_url, - path: ep.name - }, - desc: ep.desc, - // @ts-ignore - params: sortParams(ep.params.map(p => parseEPDefParam(p))), - paramDefs: extractDefs(ep.params), - }; - langs.forEach(lang => { - pug.renderFile('./src/client/docs/api/endpoints/view.pug', Object.assign({}, vars, { - lang, - title: ep.name, - src: `https://github.com/syuilo/misskey/tree/master/src/client/docs/api/endpoints/${ep.name}.yaml`, - kebab, - common: commonVars - }), (renderErr, html) => { - if (renderErr) { - console.error(renderErr); - return; - } - const i18n = new I18nReplacer(lang); - html = html.replace(i18n.pattern, i18n.replacement); - html = fa(html); - const htmlPath = `./built/client/docs/${lang}/api/endpoints/${ep.name}.html`; - mkdirp(path.dirname(htmlPath), (mkdirErr) => { - if (mkdirErr) { - console.error(mkdirErr); - return; - } - fs.writeFileSync(htmlPath, html, 'utf-8'); - }); - }); - }); - }); - }); -}); - -gulp.task('doc:api:entities', async () => { - const commonVars = await generateVars(); - glob('./src/client/docs/api/entities/**/*.yaml', (globErr, files) => { - if (globErr) { - console.error(globErr); - return; - } - files.forEach(file => { - const entity = yaml.safeLoad(fs.readFileSync(file, 'utf-8')) as any; - const vars = { - name: entity.name, - desc: entity.desc, - // WIP type - props: sortParams(entity.props.map((p: any) => parseParam(p))), - propDefs: extractDefs(entity.props), - }; - langs.forEach(lang => { - pug.renderFile('./src/client/docs/api/entities/view.pug', Object.assign({}, vars, { - lang, - title: entity.name, - src: `https://github.com/syuilo/misskey/tree/master/src/client/docs/api/entities/${kebab(entity.name)}.yaml`, - kebab, - common: commonVars - }), (renderErr, html) => { - if (renderErr) { - console.error(renderErr); - return; - } - const i18n = new I18nReplacer(lang); - html = html.replace(i18n.pattern, i18n.replacement); - html = fa(html); - const htmlPath = `./built/client/docs/${lang}/api/entities/${kebab(entity.name)}.html`; - mkdirp(path.dirname(htmlPath), (mkdirErr) => { - if (mkdirErr) { - console.error(mkdirErr); - return; - } - fs.writeFileSync(htmlPath, html, 'utf-8'); - }); - }); - }); - }); - }); -}); diff --git a/src/client/docs/api/mixins.pug b/src/client/docs/api/mixins.pug index 913135a85f..6762158f86 100644 --- a/src/client/docs/api/mixins.pug +++ b/src/client/docs/api/mixins.pug @@ -1,10 +1,10 @@ mixin propTable(props) table.props thead: tr - th %i18n:docs.api.props.name% - th %i18n:docs.api.props.type% - th %i18n:docs.api.props.optional% - th %i18n:docs.api.props.description% + th= i18n('docs.api.props.name') + th= i18n('docs.api.props.type') + th= i18n('docs.api.props.optional') + th= i18n('docs.api.props.description') tbody each prop in props tr @@ -31,7 +31,7 @@ mixin propTable(props) | (Date) td.optional if prop.optional - | %i18n:docs.api.props.yes% + = i18n('docs.api.props.yes') else - | %i18n:docs.api.props.no% - td.desc!= prop.desc[lang] || prop.desc['ja'] + = i18n('docs.api.props.no') + td.desc!= prop.desc ? prop.desc[lang] || prop.desc['ja'] : null diff --git a/src/client/docs/gulpfile.ts b/src/client/docs/gulpfile.ts index 5ef87315fb..2a95dfbfee 100644 --- a/src/client/docs/gulpfile.ts +++ b/src/client/docs/gulpfile.ts @@ -2,73 +2,14 @@ * Gulp tasks */ -import * as fs from 'fs'; -import * as path from 'path'; -import * as glob from 'glob'; import * as gulp from 'gulp'; -import * as pug from 'pug'; -import * as mkdirp from 'mkdirp'; const stylus = require('gulp-stylus'); const cssnano = require('gulp-cssnano'); -import I18nReplacer from '../../build/i18n'; -import fa from '../../build/fa'; -import generateVars from './vars'; - -//require('./api/gulpfile.ts'); - gulp.task('doc', [ - 'doc:docs', - //'doc:api', 'doc:styles' ]); -gulp.task('doc:docs', async () => { - const commonVars = await generateVars(); - - glob('./src/client/docs/**/*.*.pug', (globErr, files) => { - if (globErr) { - console.error(globErr); - return; - } - files.forEach(file => { - const [, name, lang] = file.match(/docs\/(.+?)\.(.+?)\.pug$/); - const vars = { - common: commonVars, - lang: lang, - title: fs.readFileSync(file, 'utf-8').match(/^h1 (.+?)\r?\n/)[1], - src: `https://github.com/syuilo/misskey/tree/master/src/client/docs/${name}.${lang}.pug`, - }; - pug.renderFile(file, vars, (renderErr, content) => { - if (renderErr) { - console.error(renderErr); - return; - } - - pug.renderFile('./src/client/docs/layout.pug', Object.assign({}, vars, { - content - }), (renderErr2, html) => { - if (renderErr2) { - console.error(renderErr2); - return; - } - const i18n = new I18nReplacer(lang); - html = html.replace(i18n.pattern, i18n.replacement); - html = fa(html); - const htmlPath = `./built/client/docs/${lang}/${name}.html`; - mkdirp(path.dirname(htmlPath), (mkdirErr) => { - if (mkdirErr) { - console.error(mkdirErr); - return; - } - fs.writeFileSync(htmlPath, html, 'utf-8'); - }); - }); - }); - }); - }); -}); - gulp.task('doc:styles', () => gulp.src('./src/client/docs/**/*.styl') .pipe(stylus()) diff --git a/src/server/web/docs.ts b/src/server/web/docs.ts index 849194388c..b1916abc13 100644 --- a/src/server/web/docs.ts +++ b/src/server/web/docs.ts @@ -9,6 +9,7 @@ import { Context } from 'cafy'; import ObjectContext from 'cafy/built/types/object'; import config from '../../config'; import generateVars from '../../client/docs/vars'; +import I18n from '../../build/i18n'; const docs = `${__dirname}/../../client/docs/`; @@ -63,6 +64,7 @@ router.get('/assets/*', async ctx => { }); router.get('/*/api/endpoints/*', async ctx => { + const lang = ctx.params[0]; const ep = require('../../../built/server/api/endpoints/' + ctx.params[1]).meta; const vars = { @@ -76,14 +78,16 @@ router.get('/*/api/endpoints/*', async ctx => { params: sortParams(Object.keys(ep.params).map(k => parseEPDefParam(k, ep.params[k]))), paramDefs: extractDefs(Object.keys(ep.params).map(k => ep.params[k])), }; - console.log(vars); const commonVars = await generateVars(); + const i18n = new I18n(lang); + await ctx.render('../../../../src/client/docs/api/endpoints/view', Object.assign({}, vars, { - lang: 'ja', + lang, title: ep.name, kebab: (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(), + i18n: (key: string) => i18n.get(null, key), common: commonVars })); }); diff --git a/webpack.config.ts b/webpack.config.ts index 3c426ebb49..1e49043764 100644 --- a/webpack.config.ts +++ b/webpack.config.ts @@ -19,7 +19,7 @@ const constants = require('./src/const.json'); import config from './src/config'; import { licenseHtml } from './src/build/license'; -import locales from './locales'; +const locales = require('./locales'); const meta = require('./package.json'); const version = meta.clientVersion; const codename = meta.codename; -- cgit v1.2.3-freya From 118db9b267c451a164ceaabfdb7ca16d4834e9e2 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 6 Jul 2018 20:27:48 +0900 Subject: wip --- src/client/docs/api/entities/note.yaml | 4 +- src/client/docs/api/entities/view.pug | 2 +- src/client/docs/layout.pug | 14 ++-- src/client/docs/vars.ts | 64 --------------- src/server/web/docs.ts | 143 ++++++++++++++++++++++++++++----- 5 files changed, 135 insertions(+), 92 deletions(-) delete mode 100644 src/client/docs/vars.ts (limited to 'src/server/web/docs.ts') diff --git a/src/client/docs/api/entities/note.yaml b/src/client/docs/api/entities/note.yaml index 6fd26543bb..c508dab3db 100644 --- a/src/client/docs/api/entities/note.yaml +++ b/src/client/docs/api/entities/note.yaml @@ -27,8 +27,8 @@ props: type: "string" optional: true desc: - ja: "投稿の本文 (ローカルの場合Markdown風のフォーマット)" - en: "The text of this note (in Markdown like format if local)" + ja: "投稿の本文" + en: "The text of this note" - name: "mediaIds" type: "id(DriveFile)[]" optional: true diff --git a/src/client/docs/api/entities/view.pug b/src/client/docs/api/entities/view.pug index ac938151a7..a930f71eb6 100644 --- a/src/client/docs/api/entities/view.pug +++ b/src/client/docs/api/entities/view.pug @@ -10,7 +10,7 @@ block main p#desc= desc[lang] || desc['ja'] section - h2 %i18n:docs.api.entities.properties% + h2= i18n('docs.api.entities.properties') +propTable(props) if propDefs diff --git a/src/client/docs/layout.pug b/src/client/docs/layout.pug index 1d9ebcb4cd..4186d2d365 100644 --- a/src/client/docs/layout.pug +++ b/src/client/docs/layout.pug @@ -10,24 +10,24 @@ html(lang= lang) block meta //- FontAwesome style - style #{common.facss} + style #{facss} body nav ul - each doc in common.docs + each doc in docs li: a(href=`/docs/${lang}/${doc.name}`)= doc.title[lang] || doc.title['ja'] section h2 API ul li Entities ul - each entity in common.entities - li: a(href=`/docs/${lang}/api/entities/${common.kebab(entity)}`)= entity + each entity in entities + li: a(href=`/docs/${lang}/api/entities/${kebab(entity)}`)= entity li Endpoints ul - each endpoint in common.endpoints - li: a(href=`/docs/${lang}/api/endpoints/${common.kebab(endpoint)}`)= endpoint + each endpoint in endpoints + li: a(href=`/docs/${lang}/api/endpoints/${kebab(endpoint)}`)= endpoint main article block main @@ -38,4 +38,4 @@ html(lang= lang) p | %i18n:docs.edit-this-page-on-github% a(href=src target="_blank") %i18n:docs.edit-this-page-on-github-link% - small= common.copyright + small= copyright diff --git a/src/client/docs/vars.ts b/src/client/docs/vars.ts deleted file mode 100644 index 93082767e3..0000000000 --- a/src/client/docs/vars.ts +++ /dev/null @@ -1,64 +0,0 @@ -import * as fs from 'fs'; -import * as util from 'util'; -import * as glob from 'glob'; -import * as yaml from 'js-yaml'; -import * as licenseChecker from 'license-checker'; -import * as tmp from 'tmp'; - -import { fa } from '../../build/fa'; -import config from '../../config'; -import { licenseHtml } from '../../build/license'; -const constants = require('../../const.json'); - -export default async function(): Promise<{ [key: string]: any }> { - const vars = {} as { [key: string]: any }; - - const endpoints = glob.sync('./src/client/docs/api/endpoints/**/*.yaml'); - vars['endpoints'] = endpoints.map(ep => { - const _ep = yaml.safeLoad(fs.readFileSync(ep, 'utf-8')) as any; - return _ep.endpoint; - }); - - const entities = glob.sync('./src/client/docs/api/entities/**/*.yaml'); - vars['entities'] = entities.map(x => { - const _x = yaml.safeLoad(fs.readFileSync(x, 'utf-8')) as any; - return _x.name; - }); - - const docs = glob.sync('./src/client/docs/**/*.*.pug'); - vars['docs'] = {}; - docs.forEach(x => { - const [, name, lang] = x.match(/docs\/(.+?)\.(.+?)\.pug$/); - if (vars['docs'][name] == null) { - vars['docs'][name] = { - name, - title: {} - }; - } - vars['docs'][name]['title'][lang] = fs.readFileSync(x, 'utf-8').match(/^h1 (.+?)\r?\n/)[1]; - }); - - vars['kebab'] = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(); - - vars['config'] = config; - - vars['copyright'] = constants.copyright; - - vars['facss'] = fa.dom.css(); - - vars['license'] = licenseHtml; - - const tmpObj = tmp.fileSync(); - fs.writeFileSync(tmpObj.name, JSON.stringify({ - licenseText: '' - }), 'utf-8'); - const dependencies = await util.promisify(licenseChecker.init).bind(licenseChecker)({ - start: __dirname + '/../../../', - customPath: tmpObj.name - }); - tmpObj.removeCallback(); - - vars['dependencies'] = dependencies; - - return vars; -} diff --git a/src/server/web/docs.ts b/src/server/web/docs.ts index b1916abc13..c7430e0733 100644 --- a/src/server/web/docs.ts +++ b/src/server/web/docs.ts @@ -2,17 +2,66 @@ * Docs */ +import * as fs from 'fs'; +import * as path from 'path'; import ms = require('ms'); import * as Router from 'koa-router'; import * as send from 'koa-send'; import { Context } from 'cafy'; +import * as glob from 'glob'; +import * as yaml from 'js-yaml'; import ObjectContext from 'cafy/built/types/object'; import config from '../../config'; -import generateVars from '../../client/docs/vars'; import I18n from '../../build/i18n'; +import { fa } from '../../build/fa'; +import { licenseHtml } from '../../build/license'; +const constants = require('../../const.json'); const docs = `${__dirname}/../../client/docs/`; +async function genVars(lang: string): Promise<{ [key: string]: any }> { + const vars = {} as { [key: string]: any }; + + vars['lang'] = lang; + + const endpoints = glob.sync('./built/server/api/endpoints/**/*.js'); + vars['endpoints'] = endpoints.map(ep => require('../../../' + ep)).filter(x => x.meta).map(x => x.meta.name); + + const entities = glob.sync('./src/client/docs/api/entities/**/*.yaml'); + vars['entities'] = entities.map(x => { + const _x = yaml.safeLoad(fs.readFileSync(x, 'utf-8')) as any; + return _x.name; + }); + + const docs = glob.sync('./src/client/docs/**/*.*.pug'); + vars['docs'] = {}; + docs.forEach(x => { + const [, name, lang] = x.match(/docs\/(.+?)\.(.+?)\.pug$/); + if (vars['docs'][name] == null) { + vars['docs'][name] = { + name, + title: {} + }; + } + vars['docs'][name]['title'][lang] = fs.readFileSync(x, 'utf-8').match(/^h1 (.+?)\r?\n/)[1]; + }); + + vars['kebab'] = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(); + + vars['config'] = config; + + vars['copyright'] = constants.copyright; + + vars['facss'] = fa.dom.css(); + + vars['license'] = licenseHtml; + + const i18n = new I18n(lang); + vars['i18n'] = (key: string) => i18n.get(null, key); + + return vars; +} + // WIP type const parseEPDefParam = (key: string, param: Context) => { return Object.assign({ @@ -21,6 +70,46 @@ const parseEPDefParam = (key: string, param: Context) => { }, param.data); }; +const parseParam = (param: any) => { + const id = param.type.match(/^id\((.+?)\)|^id/); + const entity = param.type.match(/^entity\((.+?)\)/); + const isObject = /^object/.test(param.type); + const isDate = /^date/.test(param.type); + const isArray = /\[\]$/.test(param.type); + if (id) { + param.kind = 'id'; + param.type = 'string'; + param.entity = id[1]; + if (isArray) { + param.type += '[]'; + } + } + if (entity) { + param.kind = 'entity'; + param.type = 'object'; + param.entity = entity[1]; + if (isArray) { + param.type += '[]'; + } + } + if (isObject) { + param.kind = 'object'; + } + if (isDate) { + param.kind = 'date'; + param.type = 'string'; + if (isArray) { + param.type += '[]'; + } + } + + if (param.optional) { + param.type += '?'; + } + + return param; +}; + const sortParams = (params: Array<{name: string}>) => { params.sort((a, b) => { if (a.name < b.name) @@ -33,7 +122,7 @@ const sortParams = (params: Array<{name: string}>) => { }; // WIP type -const extractDefs = (params: Context[]) => { +const extractEPDefs = (params: Context[]) => { let defs: any[] = []; params.forEach(param => { @@ -44,7 +133,26 @@ const extractDefs = (params: Context[]) => { params: sortParams(Object.keys(props).map(k => parseEPDefParam(k, props[k]))) }); - const childDefs = extractDefs(Object.keys(props).map(k => props[k])); + const childDefs = extractEPDefs(Object.keys(props).map(k => props[k])); + + defs = defs.concat(childDefs); + } + }); + + return sortParams(defs); +}; + +const extractDefs = (params: any[]) => { + let defs: any[] = []; + + params.forEach(param => { + if (param.def) { + defs.push({ + name: param.defName, + params: sortParams(param.def.map((p: any) => parseParam(p))) + }); + + const childDefs = extractDefs(param.def); defs = defs.concat(childDefs); } @@ -68,6 +176,7 @@ router.get('/*/api/endpoints/*', async ctx => { const ep = require('../../../built/server/api/endpoints/' + ctx.params[1]).meta; const vars = { + title: ep.name, endpoint: ep.name, url: { host: config.api_url, @@ -76,26 +185,24 @@ router.get('/*/api/endpoints/*', async ctx => { desc: ep.desc, // @ts-ignore params: sortParams(Object.keys(ep.params).map(k => parseEPDefParam(k, ep.params[k]))), - paramDefs: extractDefs(Object.keys(ep.params).map(k => ep.params[k])), + paramDefs: extractEPDefs(Object.keys(ep.params).map(k => ep.params[k])), }; - const commonVars = await generateVars(); + await ctx.render('../../../../src/client/docs/api/endpoints/view', Object.assign(await genVars(lang), vars)); +}); - const i18n = new I18n(lang); +router.get('/*/api/entities/*', async ctx => { + const lang = ctx.params[0]; + const entity = ctx.params[1]; - await ctx.render('../../../../src/client/docs/api/endpoints/view', Object.assign({}, vars, { - lang, - title: ep.name, - kebab: (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(), - i18n: (key: string) => i18n.get(null, key), - common: commonVars - })); -}); + const x = yaml.safeLoad(fs.readFileSync(path.resolve('./src/client/docs/api/entities/' + entity + '.yaml'), 'utf-8')) as any; -router.get('*', async ctx => { - await send(ctx, `${ctx.params[0]}.html`, { - root: docs - }); + await ctx.render('../../../../src/client/docs/api/entities/view', Object.assign(await genVars(lang), { + name: x.name, + desc: x.desc, + props: sortParams(x.props.map((p: any) => parseParam(p))), + propDefs: extractDefs(x.props) + })); }); export default router; -- cgit v1.2.3-freya From e74c1f2ac64be957cb43cc836d299710fd4537c5 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 6 Jul 2018 23:52:47 +0900 Subject: w --- src/server/api/call.ts | 2 +- src/server/api/endpoints/meta.ts | 2 +- src/server/web/docs.ts | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) (limited to 'src/server/web/docs.ts') diff --git a/src/server/api/call.ts b/src/server/api/call.ts index fd3cea7743..b1e1668769 100644 --- a/src/server/api/call.ts +++ b/src/server/api/call.ts @@ -31,7 +31,7 @@ export default (endpoint: string | Endpoint, user: IUser, app: IApp, data: any, } } - let exec = require(`${__dirname}/endpoints/${ep.name}`); + let exec = require(`${__dirname}/endpoints/${ep.name}`).default; if (ep.withFile && file) { exec = exec.bind(null, file); diff --git a/src/server/api/endpoints/meta.ts b/src/server/api/endpoints/meta.ts index 836123a3ae..64cf9db55d 100644 --- a/src/server/api/endpoints/meta.ts +++ b/src/server/api/endpoints/meta.ts @@ -38,7 +38,7 @@ const client = require('../../../../built/client/meta.json'); /** * Show core info */ -export default (params: any) => new Promise(async (res, rej) => { +export default () => new Promise(async (res, rej) => { const meta: any = (await Meta.findOne()) || {}; res({ diff --git a/src/server/web/docs.ts b/src/server/web/docs.ts index c7430e0733..e670709634 100644 --- a/src/server/web/docs.ts +++ b/src/server/web/docs.ts @@ -205,4 +205,11 @@ router.get('/*/api/entities/*', async ctx => { })); }); +router.get('/*/*', async ctx => { + const lang = ctx.params[0]; + const doc = ctx.params[1]; + + await ctx.render('../../../../src/client/docs/' + doc + '.' + lang, await genVars(lang)); +}); + export default router; -- cgit v1.2.3-freya