write.ls 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. require! mkdirp
  2. require! './helpers.ls'
  3. {sort-by, unique} = require \prelude-ls
  4. {select-random, slugify-db, slugify-project, in-this-category, in-this-subcategory, nested-subcategories, in-this-protocol, nested-categories-web, platform-types, protocol-types} = require './sort.ls'
  5. view-path = (view-name) ->
  6. 'source/views/' + view-name + '.pug'
  7. routes = (subdirectory, depth) ->
  8. if depth?
  9. if depth == 0
  10. prefix = './'
  11. else if depth == 1
  12. prefix = '../'
  13. else if depth == 2
  14. prefix = '../../'
  15. else
  16. console.log "depth only goes to 2"
  17. else
  18. prefix = './'
  19. bare-paths =
  20. css: '../assets/css/screen.css'
  21. about: 'about/'
  22. all: 'all/'
  23. categories: 'categories/'
  24. icons: '../assets/icons/'
  25. logos: '../assets/logos/'
  26. logos-rejected: '../assets/logos/'
  27. projects: 'projects/'
  28. protocols: 'protocols/'
  29. public: '../'
  30. root: ''
  31. subcategories: 'subcategories/'
  32. final-paths = {}
  33. for key, value of bare-paths
  34. if subdirectory == value
  35. if depth == 2
  36. final-paths[key] = '../'
  37. else
  38. final-paths[key] = '.'
  39. else
  40. final-paths[key] = prefix + value
  41. final-paths
  42. fs = require 'fs'
  43. pug = require 'pug'
  44. write-html = (view, options, file) ->
  45. options.cache = true;
  46. options.compileDebug = false;
  47. fs.writeFileSync(file + ".html", (pug.renderFile view, options))
  48. ############################################################################
  49. # WRITE FUNCTIONS
  50. # These functions write all of the HTML pages for the entire site.
  51. export write-localized-site = (db) ->
  52. mkdirp db.dir
  53. write-site-index db
  54. write-all-index db
  55. write-categories-index db
  56. write-categories-show db
  57. write-subcategories-show db
  58. write-protocols-index db
  59. write-protocols-show db
  60. write-projects-index db
  61. write-projects-show db
  62. write-about-index db
  63. write-about-media db
  64. write-site-index = (db) ->
  65. data = db.platform-types db.projects
  66. path = 'index'
  67. view = view-path path
  68. options =
  69. iso: db.iso
  70. body-class: "#{db.iso} root index"
  71. h: helpers
  72. platform-types: data
  73. path: ''
  74. routes: routes!
  75. t: db.locale
  76. file = db.dir + path
  77. write-html view, options, file
  78. write-all-index = (db) ->
  79. data = nested-subcategories db.projects, db.projects-rejected
  80. path = 'all/index'
  81. view = view-path path
  82. options =
  83. iso: db.iso
  84. body-class: "#{db.iso} all index"
  85. h: helpers
  86. subcategories: data
  87. path: 'all'
  88. routes: routes 'all', 1
  89. t: db.locale
  90. file = db.dir + path
  91. write = -> write-html view, options, file
  92. mkdirp db.dir + 'all', (err) ->
  93. if err => console.error err
  94. else write!
  95. write-categories-index = (db) ->
  96. data = db.platform-types db.projects
  97. path = 'categories/index'
  98. view = view-path path
  99. options =
  100. iso: db.iso
  101. body-class: "#{db.iso} categories index"
  102. h: helpers
  103. platform-types: data
  104. path: 'categories'
  105. routes: routes 'categories', 1
  106. t: db.locale
  107. file = db.dir + path
  108. write = ->
  109. write-html view, options, file
  110. mkdirp db.dir + 'categories', (err) ->
  111. if err => console.error err
  112. else write!
  113. write-categories-show = (db) ->
  114. create = (category) ->
  115. data = category
  116. for subcategory in data.subcategories
  117. projects-rejected = in-this-subcategory(
  118. subcategory.name,
  119. in-this-category(category.name, db.projects-rejected)
  120. )
  121. projects-rejected-web = in-this-subcategory(
  122. subcategory.name,
  123. in-this-category('Web Services', db.projects-rejected)
  124. )
  125. projects-rejected-all = sort-by(
  126. (.name.to-lower-case!),
  127. unique(projects-rejected.concat(projects-rejected-web))
  128. )
  129. subcategory.projects-rejected = projects-rejected-all
  130. path = "categories/#{category.slug}/"
  131. view = view-path 'categories/show'
  132. options =
  133. iso: db.iso
  134. body-class: "#{db.iso} categories show"
  135. h: helpers
  136. category: category
  137. platform-types: db.platform-types db.projects
  138. path: path
  139. routes: routes 'categories', 2
  140. t: db.locale
  141. full-path = db.dir + path
  142. file = full-path + 'index'
  143. write = ->
  144. write-html view, options, file
  145. mkdirp full-path, (err) ->
  146. if err
  147. console.error err
  148. else
  149. write!
  150. for category in nested-categories-web(db.projects)
  151. create category
  152. write-subcategories-show = (db) ->
  153. create = (subcategory) ->
  154. projects-rejected = in-this-subcategory(
  155. subcategory.name,
  156. in-this-category(category.name, db.projects-rejected)
  157. )
  158. projects-rejected-web = in-this-subcategory(
  159. subcategory.name,
  160. in-this-category('Web Services', db.projects-rejected)
  161. )
  162. projects-rejected-all = sort-by(
  163. (.name.to-lower-case!),
  164. unique(projects-rejected.concat(projects-rejected-web))
  165. )
  166. data =
  167. category: category
  168. subcategory: subcategory
  169. projects: subcategory.projects
  170. projects-rejected: projects-rejected-all
  171. path = "subcategories/#{category.slug}-#{subcategory.slug}/"
  172. view = view-path 'subcategories/show'
  173. options =
  174. iso: db.iso
  175. body-class: "#{db.iso} subcategories show"
  176. h: helpers
  177. data: data
  178. path: path
  179. routes: routes 'subcategories', 2
  180. t: db.locale
  181. full-path = db.dir + path
  182. file = full-path + 'index'
  183. write = ->
  184. write-html view, options, file
  185. mkdirp full-path, (err) ->
  186. if err
  187. console.error err
  188. else
  189. write!
  190. for category in nested-categories-web(db.projects)
  191. for subcategory in category.subcategories
  192. create subcategory
  193. write-protocols-index = (db) ->
  194. data = protocol-types db.protocols
  195. path = 'protocols/index'
  196. view = view-path path
  197. options =
  198. iso: db.iso
  199. body-class: "#{db.iso} protocols index"
  200. h: helpers
  201. protocol-types: data
  202. path: 'protocols'
  203. routes: routes 'protocols', 1
  204. t: db.locale
  205. file = db.dir + path
  206. write = ->
  207. write-html view, options, file
  208. mkdirp db.dir + 'protocols', (err) ->
  209. if err
  210. console.error err
  211. else
  212. write!
  213. write-protocols-show = (db) ->
  214. create = (protocol) ->
  215. protocol.projects = in-this-protocol(protocol.name, db.projects)
  216. data = protocol
  217. path = "protocols/#{protocol.slug}/"
  218. view = view-path 'protocols/show'
  219. options =
  220. iso: db.iso
  221. body-class: "#{db.iso} protocols show"
  222. h: helpers
  223. protocol: data
  224. protocol-types: protocol-types db.protocols
  225. path: path
  226. routes: routes 'protocols', 2
  227. t: db.locale
  228. full-path = db.dir + path
  229. file = full-path + 'index'
  230. write = ->
  231. write-html view, options, file
  232. mkdirp full-path, (err) ->
  233. if err
  234. console.error err
  235. else
  236. write!
  237. for protocol in db.protocols
  238. create protocol
  239. write-projects-index = (db) ->
  240. data = db.projects
  241. path = 'projects/index'
  242. view = view-path path
  243. options =
  244. iso: db.iso
  245. body-class: "#{db.iso} projects index"
  246. h: helpers
  247. projects: data
  248. path: 'projects'
  249. routes: routes 'projects', 1
  250. t: db.locale
  251. file = db.dir + path
  252. write = ->
  253. write-html view, options, file
  254. mkdirp db.dir + 'projects', (err) ->
  255. if err
  256. console.error err
  257. else
  258. write!
  259. write-projects-show = (db) ->
  260. create = (project) ->
  261. data = slugify-project project
  262. path = "projects/#{project.slug}/"
  263. view = view-path 'projects/show'
  264. options =
  265. iso: db.iso
  266. body-class: "#{db.iso} projects show"
  267. h: helpers
  268. project: data
  269. routes: routes 'projects', 2
  270. t: db.locale
  271. path: path
  272. full-path = db.dir + path
  273. file = full-path + 'index'
  274. write = ->
  275. write-html view, options, file
  276. mkdirp full-path, (err) ->
  277. if err => console.error err
  278. else write!
  279. for project in db.projects
  280. create project
  281. write-about-index = (db) ->
  282. create = ->
  283. path = 'about/index'
  284. view = view-path path
  285. options =
  286. iso: db.iso
  287. body-class: "#{db.iso} about index"
  288. h: helpers
  289. path: 'about'
  290. routes: routes 'about', 1
  291. t: db.locale
  292. file = db.dir + path
  293. write = -> write-html view, options, file
  294. mkdirp db.dir + 'about', (err) ->
  295. if err => console.error err
  296. else write!
  297. create!
  298. write-about-media = (db) ->
  299. create = ->
  300. path = 'about/media/'
  301. view = view-path 'about/media'
  302. options =
  303. iso: db.iso
  304. body-class: "#{db.iso} about media"
  305. h: helpers
  306. path: path
  307. routes: routes 'about', 2
  308. t: db.locale
  309. full-path = db.dir + path
  310. file = full-path + 'index'
  311. write = -> write-html view, options, file
  312. mkdirp full-path, (err) ->
  313. if err => console.error err
  314. else write!
  315. create!