diff options
| -rw-r--r-- | CHANGELOG.md | 4 | ||||
| -rw-r--r-- | README.mdown | 27 | ||||
| -rw-r--r-- | lib/Justicar.rb | 46 | ||||
| -rw-r--r-- | lib/Justicar/version.rb | 2 | ||||
| -rw-r--r-- | template/Rakefile | 3 | ||||
| -rw-r--r-- | template/public/favicon.png | bin | 1566 -> 0 bytes | |||
| -rw-r--r-- | template/src/articles/crab.pre.rb | 8 | ||||
| -rw-r--r-- | template/src/articles/fish.pre.rb | 8 | ||||
| -rw-r--r-- | template/src/articles/index.post.rb | 28 | ||||
| -rw-r--r-- | template/src/articles/lion.pre.rb | 8 | ||||
| -rw-r--r-- | template/src/index.html.rb | 2 | ||||
| -rw-r--r-- | template/src/script.js.rb | 6 | ||||
| -rw-r--r-- | template/src/style.css.rb | 7 |
13 files changed, 120 insertions, 29 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 5407279..948c6d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ -## [Unreleased] +## [1.0.0] - 2022-04-22 +- Added processing feature +- Updated template to showcase processing ## [0.1.1] - 2022-04-21 diff --git a/README.mdown b/README.mdown index 1254c6c..a1cb28e 100644 --- a/README.mdown +++ b/README.mdown @@ -25,25 +25,28 @@ $ rake build # <- generates the website $ rake serve # <- locally host the project ``` -### How it works: +### How it works when your run `rake serve`: -#### Source Dir: +1. Preproces special files in Source Dir +2. Process remaining files in Source Dir +3. Copy Public Dir to Build Dir +4. Write the Source Hash into Build Dir + - Note this can cause the Source Hash to overwrite files copied from the Public Dir -HTML and CSS gets compiled using Paggio while Js gets compiled using Opal. +### File types -#### Public Dir: +##### `.html.rb, .css.rb` +These files are executed and the resulting string gets saved in a file using its respective relative path as well as using its filename(sans the `.rb`) as the resulting file's name -This is where static non-generated files go such as Images. +##### `.js.rb` +These files get read and passed into the Opal parser to generate the resulting javascript. This gets saved in a file similiarly as `html.rb` files. -#### Build Dir: +##### `.pre.rb` +These are files that are executed before the file generation phase but do not get written anywhere. Here you should create a proc and store it in `Justicar::PreProcessor` (see sample project for an example). These are files generally used for templates or articles where you want a table of contents. -Build is where your generated site gets exported to. +##### `.post.rb` +These are files that are executed during the file generation phase but do not themselves get written anywhere. These files are generally used for programmatically generating multiple files (such as creating a bunch of article pages stored in the preprocessor). See sample project for an example. -## Development - -After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. - -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing diff --git a/lib/Justicar.rb b/lib/Justicar.rb index 5abf901..3dd73c1 100644 --- a/lib/Justicar.rb +++ b/lib/Justicar.rb @@ -3,16 +3,20 @@ require_relative "Justicar/version" require "paggio" require "opal" +require "ostruct" class Justicar class << self - def load_templates(dir) - Dir.each_child(dir) do |file| - if File.directory? "#{dir}/#{file}" - self.load_templates "#{dir}/#{file}" + def load_preprocessors(dir) + Dir.each_child(dir) do |full_file_name| + file_name, extension, _rb = full_file_name.split('.') + if File.directory?("#{dir}/#{full_file_name}") + self.load_preprocessors("#{dir}/#{full_file_name}") else - load "#{dir}/#{file}" + if ['pre'].include? extension + load "#{dir}/#{full_file_name}" + end end end end @@ -28,14 +32,21 @@ class Justicar Dir.each_child(dir) do |full_file_name| file_name, extension, _rb = full_file_name.split('.') if File.directory?("#{dir}/#{full_file_name}") - target[full_file_name] = {} + target[full_file_name] ||= {} self.build_source("#{dir}/#{full_file_name}", target[full_file_name]) else + # single html and css files if ['html', 'css'].include? extension File.open("#{dir}/#{full_file_name}", 'r') do |file| target[full_file_name] = instance_eval(file.read) end - elsif extension == 'js' + # does "post processing" and lets you execute custom code depending on preprocessing + elsif ['post'].include? extension + File.open("#{dir}/#{full_file_name}", 'r') do |file| + # use dir and target + instance_eval(file.read) + end + elsif ['js'].include? extension opl_file, _dot, _extension = full_file_name.partition('.') opl_file = "#{"#{dir}/".partition('/').last}#{opl_file}" target[full_file_name] = opl.build(opl_file).to_s @@ -44,7 +55,7 @@ class Justicar end end - def build(target_dir, public_dir, hash = self.output) + def build_initialize(target_dir, public_dir, hash = self.output) if Dir.exist? target_dir FileUtils.rm_r target_dir end @@ -53,6 +64,10 @@ class Justicar else FileUtils.mkdir target_dir end + build(target_dir, public_dir, hash) + end + + def build(target_dir, public_dir, hash = self.output) hash.each do |key, val| if val.is_a? String file_name, type, _rb = key.to_s.split('.') @@ -60,14 +75,19 @@ class Justicar file.write(val) end else - unless Dir.exist? "#{target_dir}/#{key}" - FileUtils.mkdir "#{target_dir}/#{key}" - end - self.build("#{target_dir}/#{key}", hash) + ensure_dir "#{target_dir}/#{key}" + self.build("#{target_dir}/#{key}", public_dir, hash[key]) end end end + def ensure_dir(dir) + unless Dir.exist? dir + FileUtils.mkdir dir + end + File.directory? dir + end + def output @output ||= {} end @@ -80,4 +100,6 @@ class Justicar end + PreProcessor = OpenStruct.new + end diff --git a/lib/Justicar/version.rb b/lib/Justicar/version.rb index 55f99c0..9c36f99 100644 --- a/lib/Justicar/version.rb +++ b/lib/Justicar/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class Justicar - VERSION = "0.1.1" + VERSION = "1.0.0" end diff --git a/template/Rakefile b/template/Rakefile index 1629ff0..941b1b5 100644 --- a/template/Rakefile +++ b/template/Rakefile @@ -3,8 +3,9 @@ require 'opal-browser' desc "Build your website" task :build do + Justicar.load_preprocessors('src') Justicar.build_source('src') - Justicar.build('build', 'public') + Justicar.build_initialize('build', 'public') end desc "Create a server and open your site in your browser" diff --git a/template/public/favicon.png b/template/public/favicon.png Binary files differdeleted file mode 100644 index 5a99253..0000000 --- a/template/public/favicon.png +++ /dev/null diff --git a/template/src/articles/crab.pre.rb b/template/src/articles/crab.pre.rb new file mode 100644 index 0000000..e306ae1 --- /dev/null +++ b/template/src/articles/crab.pre.rb @@ -0,0 +1,8 @@ +Justicar::PreProcessor.articles ||= {} # ensure the has exists + +# assign the result to a hash to be generated later +Justicar::PreProcessor.articles["crab.html"] = Paggio.html! do + h2 do + "This is an article about CRABS!" + end +end diff --git a/template/src/articles/fish.pre.rb b/template/src/articles/fish.pre.rb new file mode 100644 index 0000000..2efe82a --- /dev/null +++ b/template/src/articles/fish.pre.rb @@ -0,0 +1,8 @@ +Justicar::PreProcessor.articles ||= {} # ensure the has exists + +# assign the result to a hash to be generated later +Justicar::PreProcessor.articles["fish.html"] = Paggio.html! do + h2 do + "This is an article about FISH!" + end +end diff --git a/template/src/articles/index.post.rb b/template/src/articles/index.post.rb new file mode 100644 index 0000000..b9c9f6b --- /dev/null +++ b/template/src/articles/index.post.rb @@ -0,0 +1,28 @@ +# note: `target` will always be relative to the current directory for +# postprocessor files + +# this generates all of our preprocessed articles we stored +# note: you need to use the param `|_|` for paggio to allow us +# to use local variables in the proc +Justicar::PreProcessor.articles.each do |key, val| + target[key] = Paggio.html do |_| + _.html do + val + end + end +end + +# this creates the articles page which contains links to +# all of our articles +target["index.html"] = Paggio.html do |_| + _.html do + _.h1 do + 'Here is a list of all the articles!' + end + Justicar::PreProcessor.articles.each do |key, val| + _.p do + _.a.articles(href: "#{key}") { key.delete_suffix('.html').upcase + "!" } + end + end + end +end diff --git a/template/src/articles/lion.pre.rb b/template/src/articles/lion.pre.rb new file mode 100644 index 0000000..25871f5 --- /dev/null +++ b/template/src/articles/lion.pre.rb @@ -0,0 +1,8 @@ +Justicar::PreProcessor.articles ||= {} # ensure the has exists + +# assign the result to a hash to be generated later +Justicar::PreProcessor.articles["lion.html"] = Paggio.html! do + h2 do + "This is an article about LIONS!" + end +end diff --git a/template/src/index.html.rb b/template/src/index.html.rb index 9c34064..6c36224 100644 --- a/template/src/index.html.rb +++ b/template/src/index.html.rb @@ -2,7 +2,7 @@ Paggio.html do script type: "text/javascript", src: "script.js" link rel: "stylesheet", href: "style.css" link rel: 'icon', type: 'image/x-icon', href: 'favicon.ico' - img.justicar(src: 'justicar.png', alt: 'Justicar', height: '400px') + img.justicar(src: '/justicar.png', alt: 'Justicar', height: '400px') h1 { "Hello world from Justicar" } hr h2.paggio do diff --git a/template/src/script.js.rb b/template/src/script.js.rb index 0be5ef5..f624958 100644 --- a/template/src/script.js.rb +++ b/template/src/script.js.rb @@ -9,5 +9,11 @@ $document.ready do "Hello World from Opal!" end p.opal {"This part was generated by Opal from Javascript (:"} + h2.articles do + "Here is some articles that were generated using the processing system:" + end + p.articles do + a(href: '/articles').articles { "Articles" } + end end.append_to($document.body) end diff --git a/template/src/style.css.rb b/template/src/style.css.rb index 9a41dad..157fc2a 100644 --- a/template/src/style.css.rb +++ b/template/src/style.css.rb @@ -5,7 +5,7 @@ Paggio.css do rule '.justicar' do top 134.px - left 360.px + left 400.px opacity 0.7 position 'absolute' width 400.px @@ -20,6 +20,11 @@ Paggio.css do color :goldenrod end + rule '.articles' do + color :limegreen + max width: 400.px + end + rule 'h1' do font style: :italic end |
