UNPKG

2.83 kBtext/coffeescriptView Raw
1Node = require('./node')
2
3{whitespace} = require('../util/text')
4{unescapeQuotes} = require('../util/text')
5
6# Filter node for built-in Haml filters:
7#
8# * `:escaped`
9# * `:preserve`
10# * `:plain`
11# * `:css`
12# * `:javascript`
13# * `:cdata`
14#
15# Only the top level filter marker is a filter node, containing
16# child nodes are text nodes.
17#
18module.exports = class Filter extends Node
19
20 # Evaluate the Haml filters
21 #
22 evaluate: ->
23 @filter = @expression.match(/:(escaped|preserve|css|javascript|coffeescript|plain|cdata|coffeescript)(.*)?/)?[1]
24
25 # Render the filter
26 #
27 render: ->
28 output = []
29
30 switch @filter
31 when 'escaped'
32 output.push @markText(child.render()[0].text, true) for child in @children
33
34 when 'preserve'
35 preserve = ''
36 preserve += "#{ child.render()[0].text }
" for child in @children
37 preserve = preserve.replace(/\&\#x000A;$/, '')
38
39 output.push @markText(preserve)
40
41 when 'plain'
42 @renderFilterContent(0, output)
43
44 when 'css'
45 if @format is 'html5'
46 output.push @markText('<style>')
47 else
48 output.push @markText('<style type=\'text/css\'>')
49
50 output.push @markText(' /*<![CDATA[*/') if @format is 'xhtml'
51
52 indent = if @format is 'xhtml' then 2 else 1
53 @renderFilterContent(indent, output)
54
55 output.push @markText(' /*]]>*/') if @format is 'xhtml'
56 output.push @markText('</style>')
57
58 when 'javascript'
59 if @format is 'html5'
60 output.push @markText('<script>')
61 else
62 output.push @markText('<script type=\'text/javascript\'>')
63
64 output.push @markText(' //<![CDATA[') if @format is 'xhtml'
65
66 indent = if @format is 'xhtml' then 2 else 1
67 @renderFilterContent(indent, output)
68
69 output.push @markText(' //]]>') if @format is 'xhtml'
70 output.push @markText('</script>')
71
72 when 'cdata'
73 output.push @markText('<![CDATA[')
74 @renderFilterContent(2, output)
75 output.push @markText(']]>')
76
77 when 'coffeescript'
78 @renderFilterContent(0, output, 'run')
79
80 output
81
82 # Render the child content, but omits empty lines at the end
83 #
84 # @param [Array] output where to append the content
85 # @param [Number] indent the content indention
86 #
87 renderFilterContent: (indent, output, type = 'text') ->
88 content = []
89 empty = 0
90
91 content.push(child.render()[0].text) for child in @children
92
93 for line in content
94 if line is ''
95 empty += 1
96 else
97 switch type
98 when 'text'
99 output.push @markText("") for e in [0...empty]
100 output.push @markText("#{ whitespace(indent) }#{ line }")
101 when 'run'
102 output.push @markRunningCode("#{ unescapeQuotes(line) }")
103
104 empty = 0