When Invoking HTMLP_parse on a string that contains the following:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My LP File</title> </head> <body> <script type="text/coffeescript" data-htmlp="x.coffee"> x = 42 </script> <code data-htmlp="x.coffee"> console.log x is 42 </code> <code data-htmlp="y.coffee"> y = false </code> </body> </html>
The HTMLP_parse function should return:
{ "x.coffee" : "x = 42\nconsole.log x is 42" "y.coffee" : "y = false" }
When Invoking HTMLP_render on a string that contains the following:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My LP File</title> </head> <body> <h1> No file exports here :) </h1> </body> </html>
The HTMLP function should return:
{}
### Case 1 ### case_1_actual = htmlp_parse '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>My LP File</title></head><body><script type="text/coffeescript" data-htmlp="x.coffee">x = 42</script><code data-htmlp="x.coffee">console.log x is 42</code><code data-htmlp="y.coffee">y = false</code></body></html>' case_1_expected = { "x.coffee" : "x = 42\nconsole.log x is 42" "y.coffee" : "y = false" } assert _.isEqual( case_1_actual, case_1_expected ), 'Case 1 failure' ### Case 2 ### case_2_actual = htmlp_parse '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>My LP File</title></head><body><h1>No file exports here :)</h1></body></html>' case_2_expected = {} assert _.isEqual( case_1_actual, case_1_expected ), 'Case 2 failure'
Import the cheerio library
$ = require 'cheerio'
Define the data key and attribute name that we will be focussing on
file_export_data_key = 'htmlp' file_export_attr = 'data-' + file_export_data_key
Make an ordered list of all the nodes that have a file export call
exporting_tags = $ "[#{file_export_attr}]", html_input_string
Convert the node list to an array of easily digested file write instructions
file_writes = exporting_tags.map (index, element) -> file_name = $(element).data file_export_data_key text = $(element).text() return {file_name : file_name, text : text}
Ensure that file_writes is a normal array (and not an "array-like object")
file_writes = _.toArray file_writes
Build up a files object whose keys represent filepaths and whose values represent the aggregated contents of those files.
files = {} _.each file_writes, (element) -> if _.isString files[ element.file_name ] files[ element.file_name ] += '\n' + element.text else files[ element.file_name ] = element.text
return the files object
return files