Skip to content

Instantly share code, notes, and snippets.

@cognitom
Created March 6, 2012 04:52
Show Gist options
  • Select an option

  • Save cognitom/1983669 to your computer and use it in GitHub Desktop.

Select an option

Save cognitom/1983669 to your computer and use it in GitHub Desktop.
YinYangの変更箇所 for codeReview.cafe
# [Filters]
# thru filter
class YinYang.filter
constructor: (@args) ->
process: (val) -> val
# default filter
# http://osscafe.github.com/yinyang/english/api.html#filter|default
class YinYang.filters.default extends YinYang.filter
process: (val) -> val || @args[0] || ''
# nl2br filter
# http://osscafe.github.com/yinyang/english/api.html#filter|default
class YinYang.filters.nl2br extends YinYang.filter
process: (val) -> val.replace /\r\n|\n|\r/gim, '<br />'
# truncate filter
# http://osscafe.github.com/yinyang/english/api.html#filter|default
class YinYang.filters.truncate extends YinYang.filter
process: (val) ->
max = @args[0] || 80
txt = @args[1] || '...'
if val.length > max then val.substring(0, max - txt.length) + txt else val
YinYang =
version: '0.1.6'
plugins: {}
filters: {}
createFilter: (str) ->
args = str.split ':'
filter_name = args.shift()
args =
for arg in args
if arg.match /^[1-9][0-9]*$/
(Number) arg
else
arg.replace /^\s*('|")|("|')\s*$/g, ''
if YinYang.filters[filter_name]? then new YinYang.filters[filter_name] args else new YinYang.filter args
class TemplateVar extends Template
constructor: (@parent = null, @value = '', @ignore = false) ->
fs = @value.split '|'
@value = fs.shift()
@filters = (YinYang.createFilter f for f in fs)
@children = []
display: (localValues) ->
@localValues = localValues
v = if @value[0] == '@' then @displayDom() else @displayVar()
v = filter.process v for filter in @filters
v
displayDom: -> $(@value.substring 1).html()
displayVar: -> (@getLocalValue @value) or Template.getValue @value
getLocalValue: (combinedKey) ->
attrs = combinedKey.split '.'
tv = @localValues
tv = tv[attr] ? '' while attr = attrs.shift()
tv
re =
pend: /<!--\{end\}-->/
more: /<!--\{more\}-->/
pvar: /<!--\{(@[a-zA-Z0-9_\.\#>=\[\]]+|[a-zA-Z][a-zA-Z0-9_\.]*)(\|.*?)*\}-->/
ivar: /\#\{(@[a-zA-Z0-9_\.\#>=\[\]]+|[a-zA-Z][a-zA-Z0-9_\.]*)(\|.*?)*\}/
loop: /<!--\{[a-zA-Z][a-zA-Z0-9_\.]* in (@[a-zA-Z0-9_\.\#>=\[\]]+|[a-zA-Z][a-zA-Z0-9_\.]*)\}-->/
if value.match re.pend then @ignore = false; @parent
else if value.match re.more then @ignore = true; @
else unless @ignore
if value.match re.pvar then @_add 'child', new TemplateVar @, value.replace(/<!--{|}-->/g, ''), true
else if value.match re.ivar then @_add 'self', new TemplateVar @, value.replace /\#\{|\}/g, ''
else if value.match re.loop then @_add 'child', new TemplateLoop @, value.replace /<!--{|}-->/g, ''
else @_add 'self', new TemplateText @, value
else @
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment