Skip to content

Commit

Permalink
convert Gyazz markup -> HTML #240
Browse files Browse the repository at this point in the history
  • Loading branch information
shokai committed May 8, 2015
1 parent f1befed commit f5ccecf
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
6 changes: 5 additions & 1 deletion controllers/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
# メインコントローラモジュール
#

path = require 'path'
debug = require('debug')('gyazz:controller:main')
mongoose = require 'mongoose'
RSS = require 'rss'

GyazzMarkup = require path.resolve 'lib/markup'

Page = mongoose.model 'Page'
Pair = mongoose.model 'Pair'
Attr = mongoose.model 'Attr'
Expand Down Expand Up @@ -103,10 +106,11 @@ module.exports = (app) ->
# Limit
docs = docs.slice(0,20) if docs.length > 20

markup = new GyazzMarkup host: site_url, wiki: wiki
for page in docs
feed.item
title: page._id
description: page.text
description: markup.markup page.text, escape: false
url: "#{site_url}/#{wiki}/#{page._id}"
#guid: "" # optional - defaults to url
#categories: [] # optional - array of item categories
Expand Down
65 changes: 65 additions & 0 deletions lib/markup.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# convert Gyazz Markup to HTML
'use strict'

_ = require 'lodash'

module.exports = class GyazzMarkup

constructor: (opts) ->
@host = opts.host
@wiki = opts.wiki

markup: (lines, opts={escape: true}) ->
lines = _.escape lines if opts.escape

lines
.split /[\r\n]/
.map (line) =>
methods = [
'url_with_image'
'image'
'strong'
'url_with_title'
'url'
'wiki_link'
'inner_link'
]
for method in methods
line = @[method](line)
return line
.join ('\n')

url_with_image: (line) ->
reg = /\[{2}(https?:\/\/[^\s\[\]]+)\s(https?:\/\/[^\s\[\]]+)\.(png|jpe?g|gif|bmp)\]{2}/gi
return line unless reg.test line
line.replace reg, "<a href=\"$1\"><img src=\"$2.$3\"></a>"

image: (line) ->
reg = /\[{2,3}(https?:\/\/[^\s\[\]]+)\.(png|jpe?g|gif|bmp)\]{2,3}/gi
return line unless reg.test line
line.replace reg, "<a href=\"$1.$2\"><img src=\"$1.$2\"></a>"

strong: (line) ->
reg = /\[{3}([^\[\]]+)\]{3}/g
return line unless reg.test line
line.replace reg, "<strong>$1</strong>"

url_with_title: (line) ->
reg = /\[{2}(https?:\/\/[^\s\[\]]+)\s([^\[\]]+)\]{2}/gi
return line unless reg.test line
line.replace reg, "<a href=\"$1\">$2</a>"

url: (line) ->
reg = /\[{2}(https?:\/\/[^\s\[\]]+)\]{2}/gi
return line unless reg.test line
line.replace reg, "<a href=\"$1\">$1</a>"

wiki_link: (line) ->
reg = /\[{2}([^\/\[\]]+)::([^\[\]]+)\]{2}/gi
return line unless reg.test line
line.replace reg, "<a href=\"#{@host}/$1/\">$1</a>::<a href=\"#{@host}/$1/$2\">$2</a>"

inner_link: (line) ->
reg = /\[{2}([^\[\]]+)\]{2}/g
return line unless reg.test line
line.replace reg, "<a href=\"#{@host}/#{@wiki}/$1\">$1</a>"

0 comments on commit f5ccecf

Please sign in to comment.