├── README.md
├── fangle.coffee
└── license.txt
/README.md:
--------------------------------------------------------------------------------
1 | ## Demo website
2 |
3 | [Clicky](http://jotux.github.io/fangle/)
4 |
5 | ## Why?
6 |
7 | After reading about [Tangle](http://worrydream.com/Tangle/) I was excited about real-time interaction with code and documents but having to write javascript seemed like a pain. Fangle is an alternative way (to html/js) to create reactive documents.
8 |
9 | ## Contact/Contribute
10 |
11 | If you are interested in using fangle you can fork it on github.
12 |
13 | If you've looked at the source code and would like to scold me for how ugly it is feel free to submit issues or send pull requests.
14 |
15 | ## License
16 |
17 | All code is released under the supplied (MIT) license unless otherwise stated.
18 |
19 | ## Similar
20 |
21 | While I was working on tangle I ran across [tangledown](http://bollwyvl.github.com/TangleDown/index.html) which is similar is some ways. If you're interested in creating interactive documents from plaintext it may interest you as well.
22 |
--------------------------------------------------------------------------------
/fangle.coffee:
--------------------------------------------------------------------------------
1 | debug = false
2 | dbg = (msg) ->
3 | if debug isnt true
4 | return
5 | console.log(msg)
6 |
7 | strip = (text) ->
8 | if text isnt undefined
9 | text.replace(/^\s+|\s+/g, "")
10 | else
11 | text
12 |
13 | GetBlocks = (text) ->
14 | blocks = text.match(/(\[[a-z0-9=_]*?)\[(.*?)\](.*?\])/g)
15 | if blocks is null
16 | return null
17 | len = blocks.length - 1
18 | starts = (text.indexOf(blocks[b],0) for b in [0..len])
19 | lengths = (blocks[b].length for b in [0..len])
20 | ends = (starts[b] + lengths[b] for b in [0..len])
21 | {blocks,starts,ends}
22 |
23 | GetVariables = (blocks) ->
24 | len = blocks.length - 1
25 | names = []
26 | inits = []
27 | for b in [0..len]
28 | vars = blocks[b].match(/\[(.*?)\[/)[1].split("=")
29 | names[b] = strip(vars[0])
30 | if vars[1] isnt undefined
31 | inits[b] = strip(vars[1])
32 | else
33 | inits[b] = ""
34 | {names,inits}
35 |
36 | GetTexts = (blocks) ->
37 | len = blocks.length - 1
38 | formats = []
39 | texts = []
40 | for b in [0..len]
41 | raw = blocks[b].match(/\](.*).$/)[1]
42 | raw_split = raw.split(" ")
43 | if raw[0] is "%" and raw_split[0].length > 1
44 | formats[b] = raw_split[0]
45 | texts[b] = raw[formats[b].length..raw.length - 1]
46 | else
47 | formats[b] = ""
48 | texts[b] = raw
49 | {texts,formats}
50 |
51 | GetConfigs = (blocks) ->
52 | len = blocks.length - 1
53 | # Types are: NumberBox,AdjustableNumber,Toggle,Resultant,If,Switch,Variable
54 | # Short description: N, A, T, R, I, S, V
55 | types = []
56 | mins = []
57 | maxs = []
58 | steps = []
59 | conds = []
60 | exprs = []
61 | for b in [0..len]
62 | config = blocks[b].match(/\[.+\[(.*?)\].*\]/)
63 | types[b] = GetConfigType(config)
64 | mins[b] = ""
65 | maxs[b] = ""
66 | steps[b] = ""
67 | conds[b] = ""
68 | exprs[b] = ""
69 | switch(types[b])
70 | when "S","I"
71 | conds[b] = config[1].replace /([a-zA-Z_]+.*?)/g, (match) ->
72 | AddThisTo(match)
73 | when "R"
74 | exprs[b] = config[1].replace /([a-zA-Z_]+.*?)/g, (match) ->
75 | AddThisTo(match)
76 | when "A"
77 | range_info = config[1].split("..")
78 | step_info = range_info[1].split(",")
79 | if step_info.length > 1
80 | steps[b] = step_info[1]
81 | mins[b] = range_info[0]
82 | maxs[b] = step_info[0]
83 | else
84 | mins[b] = range_info[0]
85 | maxs[b] = range_info[1]
86 | {types,mins,maxs,steps,conds,exprs}
87 |
88 | GetConfigType = (config) ->
89 | if config[1] is ""
90 | type = "V"
91 | else if config[1] is "_"
92 | type = "N"
93 | else if config[1] is "0,1"
94 | type = "T"
95 | else if config[1].split("..").length > 1
96 | type = "A"
97 | else if /[><|&=!]/.test(config[1]) is true and config[1].split(",").length > 1
98 | type = "S"
99 | else if /[><|&=!]/.test(config[1]) is true
100 | type = "I"
101 | else if /[\*|\+|\-|\/|\^]|^\S+/.test(config[1]) is true
102 | type = "R"
103 | else
104 | type = "U"
105 |
106 | AddThisTo = (text) ->
107 | if text is undefined
108 | return
109 | text = "this.#{text}"
110 |
111 | GetHtmJs = (raw,blocks,variables,texts,configs) ->
112 | inits = ""
113 | updates = ""
114 | htm = raw[0..blocks['starts'][0] - 1]
115 | len = blocks['starts'].length - 1
116 | for b in [0..len]
117 | switch configs['types'][b]
118 | when "V"
119 | if variables['inits'][b] isnt ""
120 | inits += "#{AddThisTo(variables['names'][b])}=#{variables['inits'][b]};"
121 | else
122 | inits += "#{AddThisTo(variables['names'][b])}=0"
123 | when "R"
124 | updates += "#{AddThisTo(variables['names'][b])}=#{configs['exprs'][b]};"
125 | htm += "#{texts['texts'][b]}"
128 | when "A"
129 | if variables['inits'][b] isnt ""
130 | inits += "#{AddThisTo(variables['names'][b])}=#{variables['inits'][b]};"
131 | else
132 | inits += "#{AddThisTo(variables['names'][b])}=#{configs['mins'][b]};"
133 | htm += "#{texts['texts'][b]}"
140 | when "N"
141 | if variables['inits'][b] isnt ""
142 | inits += "#{AddThisTo(variables['names'][b])}=#{variables['inits'][b]};"
143 | else
144 | inits += "#{AddThisTo(variables['names'][b])}=0;"
145 | htm += ""
147 | when "T"
148 | if variables['inits'][b] isnt ""
149 | inits += "#{AddThisTo(variables['names'][b])}=#{variables['inits'][b]};"
150 | else
151 | inits += "#{AddThisTo(variables['names'][b])}=0;"
152 | htm += ""
154 | toggle_text = texts['texts'][b].split(",")
155 | htm += "#{toggle_text[0]}"
156 | htm += "#{toggle_text[1]}"
157 | htm += ""
158 | when "I"
159 | updates += "#{AddThisTo(variables['names'][b])}=#{configs['conds'][b]};"
160 | htm += "#{texts['texts'][b]}"
163 | when "S"
164 | switch_updates = configs['conds'][b].split(",")
165 | switch_texts = texts['texts'][b].split(",")
166 | s_len = switch_updates.length - 1
167 | htm += ""
168 | for s in [0..s_len]
169 | updates += "if (#{switch_updates[s]}){#{AddThisTo(variables['names'][b])}=#{s};}"
170 | updates += "else " if s isnt s_len
171 | htm += "#{switch_texts[s]}"
172 | htm += ""
173 | # TODO add supprt for "U"
174 | htm += raw[blocks['ends'][b]..blocks['starts'][b + 1] - 1] if (b <= len)
175 | js = "{initialize: function () {#{inits}},update: function (){#{updates}}}"
176 | {js,htm}
177 |
178 | ParseReactive = (raw) ->
179 | try
180 | dbg("Get blocks")
181 | blocks = GetBlocks(raw)
182 | if blocks is null
183 | return null
184 | dbg("Get vars and inits")
185 | variables = GetVariables(blocks['blocks'])
186 | dbg("Get text and format")
187 | texts = GetTexts(blocks['blocks'])
188 | dbg("Get config info")
189 | configs = GetConfigs(blocks['blocks'])
190 | dbg("Get js and htm")
191 | GetHtmJs(raw,blocks,variables,texts,configs)
192 | catch error
193 | dbg("Parsing Error #{error}")
194 | return null
195 |
196 | tangle = {}
197 | model = {}
198 |
199 | MdToHtml = (raw) ->
200 | try
201 | converter = new Showdown.converter()
202 | htm = converter.makeHtml(raw)
203 | catch error
204 | dbg("Markdown convertion error #{error}")
205 | return raw
206 |
207 | # Everything below this line should be abstracted out to the main page or a different script. Everything above should probably be moved into a class
208 | # Eventually I want to clean this up into a node module
209 |
210 | UpdateModel = (model) ->
211 | element = document.getElementById("t1")
212 | tangle = new Tangle(element,model)
213 |
214 | RunParse = ->
215 | raw = "\n" + $("#input").val()
216 | r = ParseReactive(raw)
217 | if r isnt null
218 | try
219 | htm = MdToHtml(r['htm'])
220 | eval("model =" + r['js'])
221 | $("#output").html(htm)
222 | UpdateModel(model)
223 | catch error
224 | dbg("Model loading error #{error}")
225 | htm = MdToHtml(raw)
226 | $("#output").html(htm)
227 | else
228 | htm = MdToHtml(raw)
229 | $("#output").html(htm)
230 |
231 | oldtext = ""
232 | newtext = ""
233 |
234 | CheckForChanges = ->
235 | newtext = $("#input").val()
236 | if newtext isnt oldtext
237 | RunParse()
238 | oldtext = newtext
239 | setTimeout CheckForChanges, 300
240 |
241 | $("#car_ex").click = ->
242 | alert("test")
243 |
244 | $ ->
245 | model =
246 | initialize: ->
247 | return
248 | update: ->
249 | return
250 | element = document.getElementById("t1")
251 | tangle = new Tangle(element,model)
252 | CheckForChanges()
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Copyright (C) 2013 Joe Brown
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 |
--------------------------------------------------------------------------------