pufferfish 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c23b78cec13deaf002496287f1302a75d49f0bcf8916b11bf0ebaf6cfe05f753
4
+ data.tar.gz: 248b77373342bddceca181ed68294a9ee31858f703dd96118f6542badebfc1ad
5
+ SHA512:
6
+ metadata.gz: 8683b990de9fa3af6c9b3227f71b2559f9dd485d8eb05f92eae3538b75b0732f745a6af51275d7acc2edd18eed541c6cfacd31a91259255bd7b81bd081204927
7
+ data.tar.gz: 46bced3ad600485d5e77747494a9546b056c2227db69a37c8958948588aebb522f436fbe3778d3304283e7320a23feb5fa1749958046a4cac27c2192e5474d96
data/bin/clinput.rb ADDED
@@ -0,0 +1,39 @@
1
+
2
+ Input = Struct.new(:cmd, :args)
3
+
4
+ # Parse CLI input
5
+ class CLI
6
+ def initialize(input)
7
+ @input = input
8
+ end
9
+
10
+ def parse
11
+ @input.each_with_index do |var, idx|
12
+ case var
13
+ when "-p" || "--pretty"
14
+ # Pretifies the html before returning
15
+ @prettify = true
16
+ when "-d" || "--base-directory"
17
+ # sets the base directory for searching files
18
+ # ! look out for absolute paths -> don't convert them
19
+ @parse_next = :base_dir
20
+ else
21
+ if @parse_next == :base_dir
22
+ @base = var
23
+ @parse_next = nil
24
+ elsif @file.nil?
25
+ @file = var
26
+ elsif @outputFile.nil?
27
+ @outputFile = var
28
+ end
29
+ end
30
+ end
31
+
32
+ vars = Hash.new
33
+ vars[:file] = @file
34
+ vars[:outputFile] = @outputFile
35
+ vars[:base_dir] = @base
36
+ vars[:pretty] = @prettify.nil? ? false : true
37
+ Input.new(:parse, vars)
38
+ end
39
+ end
@@ -0,0 +1,22 @@
1
+ require 'pufferfish'
2
+ require 'htmlbeautifier'
3
+
4
+ # Executes CLI commands
5
+ class CommandExecutor
6
+ def initialize(cmd)
7
+ @cmd = cmd
8
+ end
9
+
10
+ def execute
11
+ case @cmd.cmd
12
+ when :parse
13
+ file = @cmd.args[:file]
14
+ output = Pufferfish::Parser::parse_file(file, @cmd.args[:base_dir])
15
+ if @cmd.args[:pretty]
16
+ HtmlBeautifier.beautify(output)
17
+ else
18
+ output
19
+ end
20
+ end
21
+ end
22
+ end
data/bin/puf ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/ruby
2
+ require_relative('clinput.rb')
3
+ require_relative('command_exec.rb')
4
+
5
+ cmd = CLI.new(ARGV).parse
6
+ output = CommandExecutor.new(cmd).execute
7
+ # Check if output file was given
8
+ if cmd.args[:outputFile].nil?
9
+ puts output
10
+ else
11
+ File.write(cmd.args[:outputFile], output)
12
+ end
data/lib/pufferfish.rb ADDED
@@ -0,0 +1,72 @@
1
+ module Pufferfish
2
+ # Parser
3
+ module Parser
4
+ # Parses html
5
+ #
6
+ # ### Params
7
+ # file: String
8
+ def self.parse_file(file, base_dir)
9
+ contents = File.read(file)
10
+
11
+ # ^\\ -> don't count escape characters \%
12
+ contents = contents.gsub(/%(.*[^\\])%/) { |_|
13
+ match = Regexp.last_match.captures
14
+ file = match[0]
15
+ if not (file =~ /.*\..*/)
16
+ file.concat(".html")
17
+ # Parse base dir
18
+ if not base_dir.nil?
19
+ if file =~ /\A\/.*\z/
20
+ parse_file(file, base_dir)
21
+ elsif base_dir =~ /\A.*\/\z/
22
+ parse_file("#{base_dir}#{file}", base_dir)
23
+ else
24
+ parse_file("#{base_dir}/#{file}", base_dir)
25
+ end
26
+ else
27
+ parse_file(file, base_dir)
28
+ end
29
+ end
30
+ }
31
+
32
+ # Replace escape characters and return
33
+ contents.gsub(/\\%/, "%")
34
+ end
35
+ end
36
+
37
+ # Builder
38
+ class Builder
39
+ def initialize(fn)
40
+ builder_info = PufferfishBuilderInfo.new
41
+ answer = fn.call(builder_info)
42
+ build(builder_info)
43
+ end
44
+
45
+ def build(info)
46
+ Dir.glob("#{info.html_dir}/**/*") do |filename|
47
+ if info.verbose
48
+ STDERR.puts "compiling #{filename}..."
49
+ end
50
+ # TODO: replace with ruby calls
51
+ system "puf #{filename} #{info.output_dir}/#{filename.gsub(/\A\/?.*\//, "")} -d #{info.template_dir}" + (info.pretty ? " -p" : "")
52
+ if info.minify
53
+ file_content = `html-minifier #{info.output_dir}/#{filename.gsub(/\A\/?.*\//, "")} #{info.minify_flags}`
54
+ File.write("#{info.output_dir}/#{filename.gsub(/\A\/?.*\//, "")}", file_content)
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ class PufferfishBuilderInfo
61
+ attr_accessor :html_dir, :template_dir, :output_dir, :pretty, :minify, :minify_flags, :verbose
62
+ def initialize
63
+ @html_dir = "."
64
+ @template_dir = "."
65
+ @output_dir = "."
66
+ @pretty = false
67
+ @minify = false
68
+ @minify_flags = ""
69
+ @verbose = false
70
+ end
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pufferfish
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonas everaert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: htmlbeautifier
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.1
27
+ description: Pufferfish is a templating language for html. It generates raw html,
28
+ meaning that it will not affect load times.
29
+ email:
30
+ - info@jonaseveraert.be
31
+ executables:
32
+ - puf
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - bin/clinput.rb
37
+ - bin/command_exec.rb
38
+ - bin/puf
39
+ - lib/pufferfish.rb
40
+ homepage: https://github.com/jomy10/pufferfish
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.3.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: An html templating engine that generates static html.
63
+ test_files: []