bhook 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 004e13098155733cadddffee89a4085b4c7dc78fafa28de82b4266cd86c95eab
4
- data.tar.gz: aa6a9773afd270b0d63f230142b3ac86a3f90719b6602a7ae26cee712b36ffff
3
+ metadata.gz: 9a429bac22b3cc378e763dabbad48a1b22263160b1ce2e84a8e476f69c2f0783
4
+ data.tar.gz: bebc1d4b7ae5e570e29a8075505d8a326adc16af6abc6d62113e1cb21db4c14f
5
5
  SHA512:
6
- metadata.gz: 757d1a3bcabc583116a5d02b41beb198f19dc891cb3ce948482614bd77f7a18ae5082ee5e3e535ee777739b030d25887ae7d1b8c3ccaa566943035e7ca51cd2e
7
- data.tar.gz: e41a385694dabe87e08d21fa51a5d0091028b7317d81f48b107e2a73797d8b9eacc1eafdf52a42715149706ecbe30c604a1aab7f3cd39e3147a767a4fc6ddb85
6
+ metadata.gz: 9bbffd373a9b185635617be72154d16f358fad87b6592b1e9bb23e9295eb7a682f229dc004bcf43f1fef02e5e9838b0b15475343b2040a94b78803cbd6844e61
7
+ data.tar.gz: 2033c32dc2107c52197a6b71581970324cf0e05164948b5c83cdbf80bf0bc7665b8d3f89372e432344b6d0e23728a4b5b68a8c0bb6df40dcb079b47c6f8121a4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bhook (0.1.0)
4
+ bhook (0.1.1)
5
5
  git (~> 1.10)
6
6
  kramdown (~> 2.3)
7
7
  listen (~> 3.7)
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Bhook
2
2
 
3
- Bhook is a static bliki generator.
3
+ Bhook is a static website generator that works off a git repo containing a markdown file based wiki.
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,17 +10,29 @@ Install it yourself as:
10
10
 
11
11
  ## Usage
12
12
 
13
- TODO: Write usage instructions here
13
+ Getting help:
14
14
 
15
- ## Development
15
+ $ bhook --help
16
+ Bhook version 0.1.0
17
+ Usage: bhook --source /source/path --output /output/path
18
+ -s, --source=SOURCE Path to version controlled directory containing source md files
19
+ -o, --output=OUTPUT Path to directory where output files are to be generated
20
+ -w, --watch Continuously watch the source directory for changes and generate output
21
+ -t, --typechecking Enables sorbet runtime typechecking. Only useful when developing bhook.
22
+ -h, --help Prints this help
23
+
24
+ How I use it:
16
25
 
17
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+ ➜ essays git:(main) bhook -s . -o /tmp/out -w
27
+ ## Development
18
28
 
19
- 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).
29
+ 1. `git clone --recursive git@gitlab.com:kaiwren/bhook.git`
30
+ 2. After checking out the repo, run `bundle install` to install dependencies.
31
+ 4. Then, run `bundle exec rake` to run the specs and sorbet typechecking.
20
32
 
21
33
  ## Contributing
22
34
 
23
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bhook.
35
+ Bug reports and pull requests are welcome on Gitlab at https://gitlab.com/kaiwren/bhook.
24
36
 
25
37
  ## License
26
38
 
data/bin/bhook CHANGED
@@ -5,7 +5,12 @@ require 'bundler/setup'
5
5
  require 'bhook'
6
6
 
7
7
  puts "Bhook version #{Bhook::VERSION}"
8
- args = Bhook::ArgsParser.new(ARGV).parse!
9
- workspace = Bhook::Workspace.new(args.source, args.output)
8
+ args = Bhook::ArgsParser.new(ARGV).parse
10
9
 
10
+ if args.generate_theme
11
+ Bhook::ThemeGenerator.new(args.generate_theme).generate!
12
+ exit
13
+ end
14
+
15
+ workspace = Bhook::Workspace.new(args.source, args.output, args.theme)
11
16
  args.watch ? workspace.watch! : workspace.process!
@@ -1,23 +1,25 @@
1
1
  module Bhook
2
2
  class ArgsParser
3
- Options = Struct.new(:source, :output, :watch)
3
+ Options = Struct.new(:source, :output, :watch, :typechecking, :theme, :generate_theme)
4
4
 
5
5
  def initialize(argv)
6
- @args = Options.new(nil, nil, false)
6
+ @args = Options.new(nil, nil, false, false, nil, nil)
7
7
  @argv = argv.clone
8
8
  @opt_parser = build_opt_parser
9
9
  end
10
10
 
11
- def parse!
11
+ def parse
12
12
  begin
13
- @opt_parser.parse!(@argv)
14
- rescue OptionParser::InvalidOption => e
13
+ @opt_parser.parse(@argv)
14
+ rescue OptionParser::ParseError => e
15
15
  puts
16
16
  puts "Error! #{e.message}"
17
17
  puts
18
+ puts @opt_parser
19
+ exit
18
20
  end
19
-
20
- if !@argv.empty? || (!@args.source || !@args.output)
21
+
22
+ if source_or_output_paths_missing? && generate_theme_missing?
21
23
  puts @opt_parser
22
24
  exit
23
25
  end
@@ -25,26 +27,47 @@ module Bhook
25
27
  end
26
28
 
27
29
  private
30
+
31
+ def source_or_output_paths_missing?
32
+ !@args.source || !@args.output
33
+ end
34
+
35
+ def generate_theme_missing?
36
+ !@args.generate_theme
37
+ end
38
+
28
39
  def build_opt_parser
29
40
  OptionParser.new do |opts|
30
41
  opts.banner = "Usage: bhook --source /source/path --output /output/path"
31
42
 
32
43
  opts.on("-sSOURCE", "--source=SOURCE",
33
- String, "Path to version controlled directory containing source md files") do |s|
34
- @args.source = s
44
+ String, "Path to version controlled directory containing source md files") do |source_path|
45
+ @args.source = source_path
35
46
  end
36
47
 
37
48
  opts.on("-oOUTPUT", "--output=OUTPUT",
38
- String, "Path to directory where output files are to be generated") do |o|
39
- @args.output = o
49
+ String, "Path to directory where output files are to be generated") do |out_path|
50
+ @args.output = out_path
40
51
  end
41
52
 
42
53
  opts.on("-w", "--watch",
43
54
  FalseClass,
44
- "Continuously watch the source directory for changes and generate output") do |w|
55
+ "Continuously watch the source directory for changes and generate output") do
45
56
  @args.watch = true
46
57
  end
47
58
 
59
+ opts.on("-tPATH", "--theme=PATH",
60
+ String,
61
+ "Path to directory containing theme files to use when generating html") do |path|
62
+ @args.theme = path
63
+ end
64
+
65
+ opts.on("--generate-theme=PATH",
66
+ String,
67
+ "Generate a baseline theme that you can then modify to your needs") do |path|
68
+ @args.generate_theme = path
69
+ end
70
+
48
71
  opts.on("-h", "--help", "Prints this help") do
49
72
  puts opts
50
73
  exit
@@ -1,26 +1,29 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Bhook
5
5
  class Directory
6
+ extend T::Sig
7
+
6
8
  GIT_DIR = '.git'
7
9
  MD_EXT = '.md'
8
10
 
9
- attr_reader :sub_dirs, :md_files
10
-
11
+ sig { params(src_path: Pathname, git: T.nilable(Git::Base)).void }
11
12
  def initialize(src_path, git = nil)
12
13
  @src_path = src_path
13
14
  @git = git
14
- @sub_dirs = []
15
- @md_files = []
15
+ @sub_dirs = T.let([], T::Array[Directory])
16
+ @md_files = T.let([], T::Array[MdFile])
16
17
  build_next_level_nodes
17
18
  end
18
19
 
20
+ sig { returns(T::Boolean) }
19
21
  def root_dir_for_a_commit?
20
22
  !@git
21
23
  end
22
24
 
23
- def write!(out_path)
25
+ sig { params(out_path: String, theme: Theme).void }
26
+ def write!(out_path, theme)
24
27
  dir_path = if root_dir_for_a_commit?
25
28
  out_path
26
29
  else
@@ -28,20 +31,23 @@ module Bhook
28
31
  end
29
32
 
30
33
  FileUtils.mkdir_p(dir_path, verbose: true)
31
- @sub_dirs.each { |dir| dir.write!(dir_path) }
32
- @md_files.each { |file| file.write!(dir_path) }
34
+ @sub_dirs.each { |dir| dir.write!(dir_path, theme) }
35
+ @md_files.each { |file| file.write!(dir_path, theme) }
33
36
  end
34
-
37
+
38
+ sig { returns(T::Array[MdFile]) }
35
39
  def all_md_files
36
40
  @md_files + @sub_dirs.map(&:all_md_files).flatten
37
41
  end
38
42
 
43
+ sig { returns(String) }
39
44
  def to_s
40
45
  @src_path.to_s
41
46
  end
42
47
 
43
48
  private
44
49
 
50
+ sig { void }
45
51
  def build_next_level_nodes
46
52
  if root_dir_for_a_commit?
47
53
  @git = Git.open(@src_path)
data/lib/bhook/md_file.rb CHANGED
@@ -1,50 +1,43 @@
1
- # typed: false
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Bhook
5
5
  class MdFile
6
6
  extend T::Sig
7
- PAGE_TEMPLATE = File.read(File.join(File.dirname(__FILE__), 'theme', 'page.erb'))
8
- AFTER_H1_TEMPLATE = File.read(File.join(File.dirname(__FILE__), 'theme', '_after_h1.erb'))
9
-
7
+
8
+ PAGE_TEMPLATE = T.let(File.read(Bhook::PAGE_TEMPLATE_PATH), String)
9
+ AFTER_H1_TEMPLATE = T.let(File.read(Bhook::AFTER_H1_TEMPLATE_PATH), String)
10
+
11
+ sig { params(src_file_path: Pathname, git: Git::Base).void }
10
12
  def initialize(src_file_path, git)
11
13
  @md = T.let(File.read(src_file_path), String)
12
- @src_file_path = src_file_path.expand_path
13
- file_meta_data = git.lib.send(:command, 'log',
14
- '-n 1',
15
- '--pretty=format:%ad|%h',
16
- '--date=short',
17
- '--',
18
- @src_file_path)
19
- @src_file_date, @src_file_sha = file_meta_data.split('|')
14
+ @src_file_path = T.let(src_file_path.expand_path, Pathname)
15
+ src_file_date, src_file_sha = git.lib.send(:command, 'log',
16
+ '-n 1',
17
+ '--pretty=format:%ad|%h',
18
+ '--date=short',
19
+ '--',
20
+ @src_file_path).split('|')
21
+ @src_file_date = T.let(src_file_date, T.nilable(String))
22
+ @src_file_sha = T.let(src_file_sha, T.nilable(String))
20
23
  end
21
24
 
22
25
  sig { returns(Pathname) }
23
26
  attr_reader :src_file_path
24
27
 
25
- sig { params(out_path: String).void }
26
- def write!(out_path)
28
+ sig { params(out_path: String, theme: Bhook::Theme).void }
29
+ def write!(out_path, theme)
27
30
  out_file_name = File.basename(@src_file_path).gsub(/\.md$/, '.html')
28
31
  out_file_path = File.join(out_path, out_file_name)
29
- src_file_sha = @src_file_sha
30
- src_file_date = @src_file_date
31
- src_title = ''
32
- puts "Parsing: #{@src_file_sha} #{out_file_path}"
33
- after_h1_strategy = ->(binding_instance) { ERB.new(AFTER_H1_TEMPLATE, trim_mode: '-').result(binding_instance) }
34
-
35
- doc = Kramdown::Document.new(@md)
36
- output, warnings = Converter::Html.convert(doc.root, doc.options.merge(
37
- after_h1_strategy: after_h1_strategy,
38
- h1_callback: ->(str) { src_title = str }
39
- ))
40
- rendered_page = ERB.new(PAGE_TEMPLATE, trim_mode: '-').result(binding)
32
+ puts "Processing: #{@src_file_sha} #{@src_file_path}"
33
+ rendered_page = theme.render_page(@md, @src_file_sha, @src_file_date)
41
34
  puts "Writing: #{@src_file_sha} #{out_file_path}"
42
35
  File.write(out_file_path, rendered_page)
43
36
  end
44
37
 
45
38
  sig { returns(String) }
46
39
  def to_s
47
- @src_file_path
40
+ @src_file_path.to_s
48
41
  end
49
42
  end
50
43
  end
@@ -4,4 +4,3 @@ href="https://twitter.com/intent/tweet?text=<%= CGI.escape(src_title) -%>&via=po
4
4
  <a class="twitter-follow-button" href="https://twitter.com/ponnappa">Follow @ponnappa</a>
5
5
  <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
6
6
  </div>
7
-
@@ -0,0 +1,28 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Bhook
5
+ class Theme
6
+ extend T::Sig
7
+
8
+ sig { params(theme_path: String).void }
9
+ def initialize(theme_path)
10
+ @page_template = T.let(File.read(File.join(theme_path, 'page.erb')), String)
11
+ @after_h1_template = T.let(File.read(File.join(theme_path, '_after_h1.erb')), String)
12
+ strategy = ->(binding_instance) { ERB.new(@after_h1_template, trim_mode: '-').result(binding_instance) }
13
+ @after_h1_strategy = T.let(strategy, T.proc.params(binding_instance: Binding).returns(String))
14
+ end
15
+
16
+ sig { params(md: String, src_file_sha: T.nilable(String), src_file_date: T.nilable(String)).returns(String) }
17
+ def render_page(md, src_file_sha, src_file_date)
18
+ src_title = T.let('', String)
19
+
20
+ doc = Kramdown::Document.new(md)
21
+ output, warnings = Converter::Html.convert(doc.root, doc.options.merge(
22
+ after_h1_strategy: @after_h1_strategy,
23
+ h1_callback: ->(str) { src_title = str }
24
+ ))
25
+ ERB.new(@page_template, trim_mode: '-').result(binding)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Bhook
5
+ class ThemeGenerator
6
+ extend T::Sig
7
+
8
+ sig {params(output_path: String).void}
9
+ def initialize(output_path)
10
+ @output_path = T.let(File.join(output_path, 'theme'), String)
11
+ @template_files = T.let([Bhook::PAGE_TEMPLATE_PATH,
12
+ Bhook::AFTER_H1_TEMPLATE_PATH], T::Array[String])
13
+ end
14
+
15
+ sig {void}
16
+ def generate!
17
+ FileUtils.mkdir_p(@output_path, verbose: true)
18
+ FileUtils.cp(@template_files, @output_path, verbose: true)
19
+ end
20
+ end
21
+ end
data/lib/bhook/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Bhook
5
- VERSION = '0.1.0'
5
+ VERSION = '0.1.1'
6
6
  end
@@ -5,16 +5,17 @@ module Bhook
5
5
  class Workspace
6
6
  extend T::Sig
7
7
 
8
- sig { params(src_path: String, out_path: String).void }
9
- def initialize(src_path, out_path)
8
+ sig { params(src_path: String, out_path: String, theme_path: String).void }
9
+ def initialize(src_path, out_path, theme_path)
10
10
  @src_path = src_path
11
11
  @out_path = out_path
12
+ @theme = T.let(Theme.new(theme_path), Bhook::Theme)
12
13
  end
13
14
 
14
15
  sig { void }
15
16
  def process!
16
17
  root = root_dir
17
- root.write!(@out_path)
18
+ root.write!(@out_path, @theme)
18
19
  end
19
20
 
20
21
  sig { void }
data/lib/bhook.rb CHANGED
@@ -10,13 +10,22 @@ require 'sorbet-runtime'
10
10
  require 'git'
11
11
  require 'kramdown'
12
12
  require 'listen'
13
+
14
+ module Bhook
15
+ extend T::Sig
16
+
17
+ THEME_DIR_PATH = T.let(File.join(File.dirname(__FILE__), 'bhook', 'theme'), String)
18
+ PAGE_TEMPLATE_PATH = T.let(File.join(THEME_DIR_PATH, 'page.erb'), String)
19
+ AFTER_H1_TEMPLATE_PATH = T.let(File.join(THEME_DIR_PATH, '_after_h1.erb'), String)
20
+
21
+ class Error < StandardError; end
22
+ end
23
+
13
24
  require_relative 'bhook/version'
14
25
  require_relative 'bhook/args_parser'
26
+ require_relative 'bhook/theme_generator'
27
+ require_relative 'bhook/theme'
15
28
  require_relative 'bhook/converter/html'
16
29
  require_relative 'bhook/directory'
17
30
  require_relative 'bhook/md_file'
18
31
  require_relative 'bhook/workspace'
19
-
20
- module Bhook
21
- class Error < StandardError; end
22
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bhook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sidu Ponnappa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-12 00:00:00.000000000 Z
11
+ date: 2022-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: git
@@ -172,8 +172,10 @@ files:
172
172
  - lib/bhook/converter/html.rb
173
173
  - lib/bhook/directory.rb
174
174
  - lib/bhook/md_file.rb
175
+ - lib/bhook/theme.rb
175
176
  - lib/bhook/theme/_after_h1.erb
176
177
  - lib/bhook/theme/page.erb
178
+ - lib/bhook/theme_generator.rb
177
179
  - lib/bhook/version.rb
178
180
  - lib/bhook/workspace.rb
179
181
  - sorbet/config