From c31a93b8b5f9c8bfbd8ddd076cad775ac8fa992a Mon Sep 17 00:00:00 2001 From: jysperm Date: Tue, 2 Dec 2014 17:54:42 +0800 Subject: [PATCH] wiki plugin --- plugin/rpvhost/index.coffee | 8 ++++++ plugin/rpvhost/locale/en.json | 1 + plugin/rpvhost/locale/zh_CN.json | 1 + plugin/wiki/index.coffee | 36 ++++++++++++++----------- plugin/wiki/router.coffee | 44 ++++++++++++++++++++++++++++++ plugin/wiki/view/index.jade | 8 +++--- plugin/wiki/wiki.coffee | 46 -------------------------------- 7 files changed, 78 insertions(+), 66 deletions(-) create mode 100644 plugin/wiki/router.coffee delete mode 100644 plugin/wiki/wiki.coffee diff --git a/plugin/rpvhost/index.coffee b/plugin/rpvhost/index.coffee index 2a0a32b..009640e 100644 --- a/plugin/rpvhost/index.coffee +++ b/plugin/rpvhost/index.coffee @@ -11,6 +11,14 @@ rpvhostPlugin = module.exports = new Plugin target: '_blank' t_body: 'official_blog' + 'plugins.wiki.pages': + category: 'rpvhost' + name: 'Terms.md' + t_category: '' + t_title: 'terms' + language: 'zh_CN' + content_markdown: fs.readFileSync("#{__dirname}/wiki/Terms.md").toString() + 'billing.payment_methods': type: 'taobao' widget_generator: (req, callback) -> diff --git a/plugin/rpvhost/locale/en.json b/plugin/rpvhost/locale/en.json index 5c9317e..62cf522 100644 --- a/plugin/rpvhost/locale/en.json +++ b/plugin/rpvhost/locale/en.json @@ -3,6 +3,7 @@ "greenshadow": "GreenShadow", "taobao": "Taobao", "official_blog": "Official Blog", + "terms": "Terms Of Service", "view": { "payment_tips": "Purchase the following product, and tell us your username and which server.", "go_pay": "Pay on Taobao", diff --git a/plugin/rpvhost/locale/zh_CN.json b/plugin/rpvhost/locale/zh_CN.json index 55f5aa0..c10bfe9 100644 --- a/plugin/rpvhost/locale/zh_CN.json +++ b/plugin/rpvhost/locale/zh_CN.json @@ -3,6 +3,7 @@ "greenshadow": "GreenShadow", "taobao": "淘宝", "official_blog": "官方博客", + "terms": "服务条款", "view": { "payment_tips": "拍下对应宝贝后付款即可,购买时注意选择服务器节点选项,备注填写你的用户名。", "go_pay": "淘宝购买", diff --git a/plugin/wiki/index.coffee b/plugin/wiki/index.coffee index 8ca7d72..a42b0b0 100644 --- a/plugin/wiki/index.coffee +++ b/plugin/wiki/index.coffee @@ -1,23 +1,27 @@ {fs, path} = app.libs {pluggable, config} = app +{Plugin} = app.classes -exports = module.exports = class WikiPlugin extends pluggable.Plugin - @NAME: 'wiki' - @type: 'extension' +wikiPlugin = module.exports = new Plugin + name: 'wiki' -exports.registerHook 'view.layout.menu_bar', - href: '/wiki/' - t_body: 'plugins.wiki.' + register_hooks: + 'view.layout.menu_bar': + href: '/wiki/' + t_body: '' -unless config.plugins.wiki?.disable_default_wiki - wiki_path = "#{__dirname}/../../WIKI" + initialize: -> + unless config.plugins.wiki?.disable_default_wiki + wiki_path = "#{__dirname}/../../WIKI" - for category_name in fs.readdirSync(wiki_path) - for file_name in fs.readdirSync("#{wiki_path}/#{category_name}") - exports.registerHook 'plugin.wiki.pages', - t_category: category_name - t_title: file_name - language: 'zh_CN' - content_markdown: fs.readFileSync("#{wiki_path}/#{category_name}/#{file_name}").toString() + for category_name in fs.readdirSync(wiki_path) + for file_name in fs.readdirSync("#{wiki_path}/#{category_name}") + @registerHook 'plugins.wiki.pages', + category: category_name + name: file_name + t_category: category_name + t_title: file_name + language: 'zh_CN' + content_markdown: fs.readFileSync("#{wiki_path}/#{category_name}/#{file_name}").toString() -app.express.use '/wiki', require './wiki' + app.express.use '/wiki', require './router' diff --git a/plugin/wiki/router.coffee b/plugin/wiki/router.coffee new file mode 100644 index 0000000..70512f6 --- /dev/null +++ b/plugin/wiki/router.coffee @@ -0,0 +1,44 @@ +{markdown, path, jade, fs, _, express} = app.libs +{pluggable} = app + +wikiPlugin = null + +process.nextTick -> + wikiPlugin = require './index' + +module.exports = exports = express.Router() + +exports.get '/', (req, res) -> + pages_by_category = {} + + for page in pluggable.selectHook 'plugins.wiki.pages' + pages_by_category[page.category] ?= [] + pages_by_category[page.category].push page + + categories = [] + + for category, pages of pages_by_category + categories.push + t_name: _.first(pages).t_category + name: category + pages: pages + + wikiPlugin.render 'index', req, {categories: categories}, (html) -> + res.send html + +exports.get '/:category/:name', (req, res) -> + page = _.findWhere pluggable.selectHook('plugins.wiki.pages'), + category: req.params.category + name: req.params.name + + unless page + return res.status(404).end() + + console.log page + + view_data = + title: page.plugin.getTranslator(req) page.t_title + content: markdown.toHTML page.content_markdown + + wikiPlugin.render 'page', req, view_data, (html) -> + res.send html diff --git a/plugin/wiki/view/index.jade b/plugin/wiki/view/index.jade index cf0301c..3cd7a5f 100644 --- a/plugin/wiki/view/index.jade +++ b/plugin/wiki/view/index.jade @@ -4,10 +4,10 @@ prepend header title #{t('')} | #{t(config.web.t_name)} block main - for category in category_list - h2= category.category + for category in categories + h2= category.pages[0].plugin.getTranslator(req)(category.t_name) ul for page in category.pages li - a(href="/wiki/#{category.t_category}/#{page.t_title}")= page.title - |   (#{page.language}) + a(href="/wiki/#{category.name}/#{page.name}")= page.plugin.getTranslator(req)(page.t_title) + small   (#{page.language}) diff --git a/plugin/wiki/wiki.coffee b/plugin/wiki/wiki.coffee deleted file mode 100644 index 86908d6..0000000 --- a/plugin/wiki/wiki.coffee +++ /dev/null @@ -1,46 +0,0 @@ -{markdown, path, jade, fs, _, express} = app.libs -{pluggable} = app - -WikiPlugin = require './index' - -module.exports = exports = express.Router() - -exports.get '/', (req, res) -> - pages = pluggable.selectHook req.account, 'plugin.wiki.pages' - - pages_by_category = {} - - for page in pages - page.title = res.t page.t_title - - pages_by_category[page.t_category] ?= [] - pages_by_category[page.t_category].push page - - result = [] - - for category_name, pages of pages_by_category - result.push - t_category: category_name - category: res.t category_name - pages: pages - - view_data = _.extend res.locals, - category_list: result - - WikiPlugin.render 'index', req, view_data, (html) -> - res.send html - -exports.get '/:category/:title', (req, res) -> - matched_page = _.findWhere pluggable.selectHook(req.account, 'plugin.wiki.pages'), - t_category: req.params.category - t_title: req.params.title - - unless matched_page - return res.status(404).end() - - view_data = _.extend res.locals, - title: res.t matched_page.t_title - content: markdown.toHTML matched_page.content_markdown - - WikiPlugin.render 'page', req, view_data, (html) -> - res.send html