ottogen 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d2c2145453e01eb5836e6a495eb9a536406c6ced7dd5114e30a298aece2fa42
4
- data.tar.gz: f1b3637628cb624f3ca325498b65c8b9e3d60b7cf0597bde35f1c19bffbad357
3
+ metadata.gz: 9b34377b90101dd7d052eebf2846afb77637ddfbd4e6e21015a93237d0ef4a20
4
+ data.tar.gz: 311b1d378b4bcaa1a6b220f01a1fe17fdcc05628a4bac5f76a0e368d076b81cc
5
5
  SHA512:
6
- metadata.gz: 4170d7093922f3c13d4a163cd1a60d7e7dcac8092c56391f3a5bec18f01db1478e646906307d885a873a4ad06d2bb501181aa82de53fc55653085189a4644e75
7
- data.tar.gz: 8dc0552b797674bb2223a95b5d2167813f17d83e136999c747bfd2137c48aa923a513ab3e2ad37766c67b3774fb9801cfd8ec774eb98dc1dc3dd7a9138bdc650
6
+ metadata.gz: 0a3f16584748a1670aa78f3f0303a4332003ffd3f25891d3e433471a07b671113e705b53b5a073b249d1c817510942331e5ba005728762c0e7d873997859f86e
7
+ data.tar.gz: 86cbc9e917ec3d3f4a9d8c82148c7dddebc1d4f06043a085a9d2395e8c986cd09f134351a01dde64fd16a455cc241ba926b3941b88924d062a1941f5b2b4c8d5
data/lib/ottogen/otto.rb CHANGED
@@ -1,12 +1,11 @@
1
1
  require 'thor'
2
- require 'webrick'
3
2
  require_relative './ottogen'
4
3
 
5
4
  module Ottogen
6
5
  class Otto < Thor
7
- desc "init", "Initialize static site"
8
- def init
9
- Ottogen.init
6
+ desc "init [DIR]", "Initialize a new otto static site in DIR (defaults to the current directory)"
7
+ def init(dir=nil)
8
+ Ottogen.init(dir)
10
9
  end
11
10
 
12
11
  desc "build", "Build the static site"
@@ -14,6 +13,15 @@ module Ottogen
14
13
  Ottogen.build
15
14
  end
16
15
 
16
+ map "b" => :build
17
+
18
+ desc "generate PAGE", "Generate a new page"
19
+ def generate(page)
20
+ Ottogen.generate(page)
21
+ end
22
+
23
+ map "g" => :generate
24
+
17
25
  desc "clean", "Clean the static site"
18
26
  def clean
19
27
  Ottogen.clean
@@ -26,10 +34,9 @@ module Ottogen
26
34
 
27
35
  desc "serve", "Serve the static site"
28
36
  def serve
29
- root = File.expand_path("#{Dir.pwd}/#{Ottogen::BUILD_DIR}")
30
- server = WEBrick::HTTPServer.new :Port => 8778, :DocumentRoot => root
31
- trap 'INT' do server.shutdown end
32
- server.start
37
+ Ottogen.serve
33
38
  end
39
+
40
+ map "s" => :serve
34
41
  end
35
42
  end
@@ -1,45 +1,88 @@
1
1
  require 'asciidoctor'
2
2
  require 'fileutils'
3
3
  require 'listen'
4
+ require 'webrick'
4
5
 
5
6
  module Ottogen
6
7
  class Ottogen
7
8
  BUILD_DIR = '_build'.freeze
9
+ CONFIG = <<~YAML
10
+ title: "Otto site"
11
+ YAML
8
12
  WELCOME = <<~ADOC
9
13
  = Welcome to Otto!
10
14
 
11
15
  Otto is a static site generator that uses AsciiDoc as a markup language.
12
16
  ADOC
13
17
 
14
- def self.init
15
- puts "✨ ..."
16
- File.write("index.adoc", WELCOME)
18
+ def self.init(dir)
19
+ puts "✨ Initializing static site..."
20
+ if !dir.nil? and Dir.exist?(dir)
21
+ puts "❌ Error: Directory already exists"
22
+ exit(1)
23
+ end
24
+
25
+ if dir.nil?
26
+ files_in_current_dir = Dir.glob('**/*')
27
+ if !files_in_current_dir.empty?
28
+ puts "❌ Error: Directory must be empty"
29
+ exit(1)
30
+ end
31
+ init_in_current_dir
32
+ else
33
+ init_with_dir(dir)
34
+ end
35
+
17
36
  puts "✅"
18
37
  end
19
38
 
20
39
  def self.build
21
- puts "🔨 ..."
40
+ puts "🔨 Building static site..."
41
+ error_if_not_otto_project
22
42
  Dir.mkdir(BUILD_DIR) unless Dir.exist?(BUILD_DIR)
23
- Dir.glob('**/*.adoc').map do |name|
43
+ FileUtils.cp_r 'assets/', "#{BUILD_DIR}/assets"
44
+ Dir.glob('pages/**/*.adoc').map do |name|
24
45
  name.split('.').first
25
46
  end.each do |doc|
47
+ page = doc.sub(/^pages\//, '')
26
48
  Asciidoctor.convert_file "#{doc}.adoc",
27
49
  safe: :safe,
28
50
  mkdirs: true,
29
- to_file: "#{BUILD_DIR}/#{doc}.html"
51
+ to_file: "#{BUILD_DIR}/#{page}.html"
30
52
  end
31
53
  puts "✅"
32
54
  end
33
55
 
56
+ def self.generate(page)
57
+ puts "📝 Generating a new page..."
58
+ error_if_not_otto_project
59
+ page_title = page.split('-').map(&:capitalize).join(' ')
60
+ File.write("pages/#{page}.adoc", "= #{page_title}\n")
61
+ end
62
+
34
63
  def self.clean
35
- puts "🧽 ..."
64
+ puts "🧽 Cleaning build directory..."
65
+ error_if_not_otto_project
36
66
  return unless Dir.exist?(BUILD_DIR)
37
67
  FileUtils.rmtree(BUILD_DIR)
38
68
  puts "✅"
39
69
  end
40
70
 
71
+ def self.serve
72
+ puts "🤖 Starting server..."
73
+ error_if_not_otto_project
74
+ root = File.expand_path("#{Dir.pwd}/#{Ottogen::BUILD_DIR}")
75
+ server = WEBrick::HTTPServer.new :Port => 8778, :DocumentRoot => root
76
+ trap 'INT' do server.shutdown end
77
+ server.start
78
+ rescue Errno::EADDRINUSE
79
+ puts "❌ Server port already in use"
80
+ exit(1)
81
+ end
82
+
41
83
  def self.watch
42
84
  puts "👀 Watching files..."
85
+ error_if_not_otto_project
43
86
  listener = Listen.to(Dir.pwd, ignore: [/_build/]) do |modified, added, removed|
44
87
  puts(modified: modified, added: added, removed: removed)
45
88
  build
@@ -47,5 +90,31 @@ ADOC
47
90
  listener.start
48
91
  sleep
49
92
  end
93
+
94
+ private
95
+
96
+ def self.init_with_dir(dir)
97
+ Dir.mkdir(dir)
98
+ FileUtils.touch("#{dir}/.otto")
99
+ File.write("#{dir}/config.yml", CONFIG)
100
+ FileUtils.mkdir_p("#{dir}/assets")
101
+ FileUtils.mkdir_p("#{dir}/pages")
102
+ File.write("#{dir}/pages/index.adoc", WELCOME)
103
+ end
104
+
105
+ def self.init_in_current_dir
106
+ FileUtils.touch(".otto")
107
+ File.write("config.yml", CONFIG)
108
+ FileUtils.mkdir_p("assets")
109
+ FileUtils.mkdir_p("pages")
110
+ File.write("pages/index.adoc", WELCOME)
111
+ end
112
+
113
+ def self.error_if_not_otto_project
114
+ if !File.exist?(".otto")
115
+ puts "❌ Error: Current directory is not an otto project"
116
+ exit(1)
117
+ end
118
+ end
50
119
  end
51
120
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ottogen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Johnson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-04-17 00:00:00.000000000 Z
11
+ date: 2025-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec