1+ /*
2+
3+ Package jsx allows you to render blocks of HTML as myitcv.io/react elements.
4+ It is a temporary runtime solution for what will become a compile-time
5+ transpilation, much like JSX's relationship with Javascript.
6+
7+ For more information see https://github.com/myitcv/react/wiki
8+
9+ */
110package jsx
211
312import (
413 "fmt"
514 "strings"
615
16+ "myitcv.io/react"
17+
718 "github.com/russross/blackfriday"
819
920 "golang.org/x/net/html"
1021 "golang.org/x/net/html/atom"
11- r "myitcv.io/react"
1222)
1323
1424// each of the parse* functions does zero validation
@@ -17,49 +27,49 @@ import (
1727
1828// TODO code generate these parse functions
1929
20- func parseP (n * html.Node ) * r .PElem {
21- var kids []r .Element
30+ func parseP (n * html.Node ) * react .PElem {
31+ var kids []react .Element
2232
2333 // TODO attributes
2434
2535 for c := n .FirstChild ; c != nil ; c = c .NextSibling {
2636 kids = append (kids , parse (c ))
2737 }
2838
29- return r .P (nil , kids ... )
39+ return react .P (nil , kids ... )
3040}
3141
32- func parseHr (n * html.Node ) * r .HrElem {
42+ func parseHr (n * html.Node ) * react .HrElem {
3343 // TODO attributes
3444
35- return r .Hr (nil )
45+ return react .Hr (nil )
3646}
3747
38- func parseBr (n * html.Node ) * r .BrElem {
48+ func parseBr (n * html.Node ) * react .BrElem {
3949 // TODO attributes
4050
41- return r .Br (nil )
51+ return react .Br (nil )
4252}
4353
44- func parseH1 (n * html.Node ) * r .H1Elem {
45- var kids []r .Element
54+ func parseH1 (n * html.Node ) * react .H1Elem {
55+ var kids []react .Element
4656
4757 // TODO attributes
4858
4959 for c := n .FirstChild ; c != nil ; c = c .NextSibling {
5060 kids = append (kids , parse (c ))
5161 }
5262
53- return r .H1 (nil , kids ... )
63+ return react .H1 (nil , kids ... )
5464}
5565
56- func parseSpan (n * html.Node ) * r .SpanElem {
57- var kids []r .Element
66+ func parseSpan (n * html.Node ) * react .SpanElem {
67+ var kids []react .Element
5868
59- var vp * r .SpanProps
69+ var vp * react .SpanProps
6070
6171 if len (n .Attr ) > 0 {
62- vp = new (r .SpanProps )
72+ vp = new (react .SpanProps )
6373
6474 for _ , a := range n .Attr {
6575 switch a .Key {
@@ -77,16 +87,16 @@ func parseSpan(n *html.Node) *r.SpanElem {
7787 kids = append (kids , parse (c ))
7888 }
7989
80- return r .Span (vp , kids ... )
90+ return react .Span (vp , kids ... )
8191}
8292
83- func parseI (n * html.Node ) * r .IElem {
84- var kids []r .Element
93+ func parseI (n * html.Node ) * react .IElem {
94+ var kids []react .Element
8595
86- var vp * r .IProps
96+ var vp * react .IProps
8797
8898 if len (n .Attr ) > 0 {
89- vp = new (r .IProps )
99+ vp = new (react .IProps )
90100
91101 for _ , a := range n .Attr {
92102 switch a .Key {
@@ -104,16 +114,16 @@ func parseI(n *html.Node) *r.IElem {
104114 kids = append (kids , parse (c ))
105115 }
106116
107- return r .I (vp , kids ... )
117+ return react .I (vp , kids ... )
108118}
109119
110- func parseFooter (n * html.Node ) * r .FooterElem {
111- var kids []r .Element
120+ func parseFooter (n * html.Node ) * react .FooterElem {
121+ var kids []react .Element
112122
113- var vp * r .FooterProps
123+ var vp * react .FooterProps
114124
115125 if len (n .Attr ) > 0 {
116- vp = new (r .FooterProps )
126+ vp = new (react .FooterProps )
117127
118128 for _ , a := range n .Attr {
119129 switch a .Key {
@@ -131,16 +141,16 @@ func parseFooter(n *html.Node) *r.FooterElem {
131141 kids = append (kids , parse (c ))
132142 }
133143
134- return r .Footer (vp , kids ... )
144+ return react .Footer (vp , kids ... )
135145}
136146
137- func parseDiv (n * html.Node ) * r .DivElem {
138- var kids []r .Element
147+ func parseDiv (n * html.Node ) * react .DivElem {
148+ var kids []react .Element
139149
140- var vp * r .DivProps
150+ var vp * react .DivProps
141151
142152 if len (n .Attr ) > 0 {
143- vp = new (r .DivProps )
153+ vp = new (react .DivProps )
144154
145155 for _ , a := range n .Attr {
146156 switch a .Key {
@@ -160,16 +170,16 @@ func parseDiv(n *html.Node) *r.DivElem {
160170 kids = append (kids , parse (c ))
161171 }
162172
163- return r .Div (vp , kids ... )
173+ return react .Div (vp , kids ... )
164174}
165175
166- func parseButton (n * html.Node ) * r .ButtonElem {
167- var kids []r .Element
176+ func parseButton (n * html.Node ) * react .ButtonElem {
177+ var kids []react .Element
168178
169- var vp * r .ButtonProps
179+ var vp * react .ButtonProps
170180
171181 if len (n .Attr ) > 0 {
172- vp = new (r .ButtonProps )
182+ vp = new (react .ButtonProps )
173183
174184 for _ , a := range n .Attr {
175185 switch a .Key {
@@ -187,40 +197,40 @@ func parseButton(n *html.Node) *r.ButtonElem {
187197 kids = append (kids , parse (c ))
188198 }
189199
190- return r .Button (vp , kids ... )
200+ return react .Button (vp , kids ... )
191201}
192202
193- func parseCode (n * html.Node ) * r .CodeElem {
194- var kids []r .Element
203+ func parseCode (n * html.Node ) * react .CodeElem {
204+ var kids []react .Element
195205
196206 // TODO attributes
197207
198208 for c := n .FirstChild ; c != nil ; c = c .NextSibling {
199209 kids = append (kids , parse (c ))
200210 }
201211
202- return r .Code (nil , kids ... )
212+ return react .Code (nil , kids ... )
203213}
204214
205- func parseH3 (n * html.Node ) * r .H3Elem {
206- var kids []r .Element
215+ func parseH3 (n * html.Node ) * react .H3Elem {
216+ var kids []react .Element
207217
208218 // TODO attributes
209219
210220 for c := n .FirstChild ; c != nil ; c = c .NextSibling {
211221 kids = append (kids , parse (c ))
212222 }
213223
214- return r .H3 (nil , kids ... )
224+ return react .H3 (nil , kids ... )
215225}
216226
217- func parseImg (n * html.Node ) * r .ImgElem {
218- var kids []r .Element
227+ func parseImg (n * html.Node ) * react .ImgElem {
228+ var kids []react .Element
219229
220- var vp * r .ImgProps
230+ var vp * react .ImgProps
221231
222232 if len (n .Attr ) > 0 {
223- vp = new (r .ImgProps )
233+ vp = new (react .ImgProps )
224234
225235 for _ , a := range n .Attr {
226236 switch a .Key {
@@ -238,16 +248,16 @@ func parseImg(n *html.Node) *r.ImgElem {
238248 kids = append (kids , parse (c ))
239249 }
240250
241- return r .Img (vp , kids ... )
251+ return react .Img (vp , kids ... )
242252}
243253
244- func parseA (n * html.Node ) * r .AElem {
245- var kids []r .Element
254+ func parseA (n * html.Node ) * react .AElem {
255+ var kids []react .Element
246256
247- var vp * r .AProps
257+ var vp * react .AProps
248258
249259 if len (n .Attr ) > 0 {
250- vp = new (r .AProps )
260+ vp = new (react .AProps )
251261
252262 for _ , a := range n .Attr {
253263 switch a .Key {
@@ -265,12 +275,12 @@ func parseA(n *html.Node) *r.AElem {
265275 kids = append (kids , parse (c ))
266276 }
267277
268- return r .A (vp , kids ... )
278+ return react .A (vp , kids ... )
269279}
270280
271281// TODO replace with proper parser
272- func parseCSS (s string ) * r .CSS {
273- res := new (r .CSS )
282+ func parseCSS (s string ) * react .CSS {
283+ res := new (react .CSS )
274284
275285 parts := strings .Split (s , ";" )
276286
@@ -303,10 +313,10 @@ func parseCSS(s string) *r.CSS {
303313 return res
304314}
305315
306- func parse (n * html.Node ) r .Element {
316+ func parse (n * html.Node ) react .Element {
307317 switch n .Type {
308318 case html .TextNode :
309- return r .S (n .Data )
319+ return react .S (n .Data )
310320 case html .ElementNode :
311321 // we will fall out from here...
312322 default :
@@ -345,16 +355,16 @@ func parse(n *html.Node) r.Element {
345355 }
346356}
347357
348- var htmlCache = make (map [string ][]r .Element )
358+ var htmlCache = make (map [string ][]react .Element )
349359
350- // HTML is a runtime JSX-like parser . It parses the supplied HTML string into
360+ // HTML is a runtime JSX-like parsereact . It parses the supplied HTML string into
351361// myitcv.io/react element values. It exists as a stop-gap runtime solution to
352- // full JSX-like support within the GopherJS compiler . It should only be used
362+ // full JSX-like support within the GopherJS compilereact . It should only be used
353363// where the argument is a compile-time constant string (TODO enforce this
354364// within reactVet). HTML will panic in case s cannot be parsed as a valid HTML
355365// fragment
356366//
357- func HTML (s string ) []r .Element {
367+ func HTML (s string ) []react .Element {
358368 s = strings .TrimSpace (s )
359369
360370 if v , ok := htmlCache [s ]; ok {
@@ -373,7 +383,7 @@ func HTML(s string) []r.Element {
373383 panic (fmt .Errorf ("failed to parse HTML %q: %v" , s , err ))
374384 }
375385
376- res := make ([]r .Element , len (elems ))
386+ res := make ([]react .Element , len (elems ))
377387
378388 for i , v := range elems {
379389 res [i ] = parse (v )
@@ -388,7 +398,7 @@ func HTML(s string) []r.Element {
388398// element is expected. HTMLElem will panic if more than one HTML element
389399// results
390400//
391- func HTMLElem (s string ) r .Element {
401+ func HTMLElem (s string ) react .Element {
392402 res := HTML (s )
393403
394404 if v := len (res ); v != 1 {
@@ -401,12 +411,12 @@ func HTMLElem(s string) r.Element {
401411// Markdown is a runtime JSX-like parser for markdown. It parses the supplied
402412// markdown string into an HTML string and then hands off to the HTML function.
403413// Like the HTML function, it exists as a stop-gap runtime solution to full
404- // JSX-like support within the GopherJS compiler . It should only be used where
414+ // JSX-like support within the GopherJS compilereact . It should only be used where
405415// the argument is a compile-time constant string (TODO enforce this within
406416// reactVet). Markdown will panic in case the markdown string s results in an
407417// invalid HTML string
408418//
409- func Markdown (s string ) []r .Element {
419+ func Markdown (s string ) []react .Element {
410420
411421 h := blackfriday .MarkdownCommon ([]byte (s ))
412422
0 commit comments