This is mine. I decided to use erb since its not really much different than using handlebars.
Best part is if you don't use any ruby's gems other than standard easy to put on a lambda and use CodeBuild and CodePipeline to automatically deploy changes to S3 Static Website Hosting.
For my use case I have to compile at least a thousand pages and doing it this way is under a 1 minute
require'erb'require'json'require'fileutils'classNamespacedefinitialize(hash)hash.eachdo|key,value|singleton_class.send(:define_method,key){value}endenddefget_bindingbindingendendclassGeneratordefself.rendersrc_path,data={}html=ERB.newFile.read(src_path),nil,'-'ns=Namespace.newdatahtml.result(ns.get_binding)enddefself.render_filesrc_path,build_path,data={}html=ERB.newFile.read(src_path),nil,'-'ns=Namespace.newdataFile.openbuild_path,'w'do|f|f.writehtml.result(ns.get_binding)endenddefself.src_pathnamecwd=File.dirname__FILE__File.joincwd,'src',"erb/#{name}.html.erb"enddefself.build_pathnameputs"+ build/#{name}.html"cwd=File.dirname__FILE__path=File.joincwd,'build',"#{name}.html"ifFile.exists?(path)File.deletepathelseFileUtils.mkdir_pFile.dirname(path)endpathenddefself.json_datanamecwd=File.dirname__FILE__path=File.joincwd,'data',"#{name}.json"json=File.read(path)JSON.parse(json)end# when dealing with a file where each line is jsondefself.json_array_datanamecwd=File.dirname__FILE__path=File.joincwd,'data',"#{name}.json"data=[]File.foreach(path).with_indexdo|line,line_num|data<<JSON.parse(line)enddataendendclassPrepAnywhereGenerator<Generatordefself.headsrc_path=self.src_path('head')data={}self.rendersrc_path,dataenddefself.headersrc_path=self.src_path('header')data={}self.rendersrc_path,dataenddefself.footersrc_path=self.src_path('footer')data={}self.rendersrc_path,dataend# /defself.homepagesrc_path=self.src_path('homepage')build_path=self.build_path('index')data={head: self.head,header: self.header,footer: self.footer,data: self.json_data('homepage')}self.render_filesrc_path,build_path,dataend# /textbooks# /textbooks/us# /textbooks/canadadefself.textbookssrc_path=self.src_path('textbooks')textbooks_all=self.json_data('textbooks')textbooks_ca=self.json_data('textbooks-ca')textbooks_us=self.json_data('textbooks-us')# Canada Booksbuild_path_all=self.build_path('textbooks/index')build_path_ca=self.build_path('textbooks/ca')build_path_us=self.build_path('textbooks/us')data={head: self.head,header: self.header,footer: self.footer,}self.render_filesrc_path,build_path_all,data.merge({body_class: 'textbooks-all',data: textbooks_all})self.render_filesrc_path,build_path_us,data.merge({body_class: 'textbooks-us',data: textbooks_us})self.render_filesrc_path,build_path_ca,data.merge({body_class: 'textbooks-ca',data: textbooks_ca})end# /textbooks/:bookdefself.textbookdata_key='textbook'src_path=self.src_path('textbook')books=self.json_array_data(data_key)books.eachdo|book|path=['textbooks',book['permalink'],'index'].join('/')build_path=self.build_pathpathdata={head: self.head,header: self.header,footer: self.footer,book: book}self.render_filesrc_path,build_path,dataendend# /textbooks/:book/chapters/:chapter/materials/:materialdefself.materialdata_key='material'src_path=self.src_path('material')materials=self.json_array_data(data_key)materials.eachdo|material|build_path=self.build_path(self.material_path(material))data={head: self.head,header: self.header,footer: self.footer,material: material}self.render_filesrc_path,build_path,dataendenddefself.material_pathmaterial['textbooks',material['textbook_permalink'],'chapters',material['chapter_permalink'],'materials',material['permalink'],'index'].join('/')end# /textbooks/:book/chapters/:chapter/materials/:material/videos/:videodefself.videodata_key='video'src_path=self.src_path('video')videos=self.json_array_data(data_key)videos.eachdo|video|build_path=self.build_path(self.video_path(video))data={head: self.head,header: self.header,footer: self.footer,video: video}self.render_filesrc_path,build_path,dataendenddefself.video_pathvideo['textbooks',video['textbook_permalink'],'chapters',video['chapter_permalink'],'materials',video['material_permalink'],'videos',video['permalink'],].join('/')endend
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This is mine. I decided to use erb since its not really much different than using handlebars.
Best part is if you don't use any ruby's gems other than standard easy to put on a lambda and use CodeBuild and CodePipeline to automatically deploy changes to S3 Static Website Hosting.
For my use case I have to compile at least a thousand pages and doing it this way is under a 1 minute