jewfish 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 82320fc3c3d9c45a5ff211499496f2943fb2ef70
4
+ data.tar.gz: 4a3f1e59901fdad0f80221855402717364f6876c
5
+ SHA512:
6
+ metadata.gz: be9c15c122922512130efb61c435e60a6a994231953ad6c2a4218e8fad7b7875b298c6784a8b6aa4e81a503e7f8a57516eb18b2e1e7ad71543fd5590b1e07653
7
+ data.tar.gz: 50dcbe55a9ebf363995238fd01cd05d96ca11e13aa3fba938cda0bdb4204c9b47397fd8c3e37c85c6460c0f7e37b4fc4be72827dab3fece75f280d6d29cfab86
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.0.0
5
+ - 1.9.3
6
+ script:
7
+ - bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jewfish.gemspec
4
+ gemspec
@@ -0,0 +1,52 @@
1
+ [![Build Status](https://img.shields.io/travis/unak/jewfish.svg)](https://travis-ci.org/unak/jewfish)
2
+ [![Version ](https://img.shields.io/gem/v/jewfish.svg)](https://rubygems.org/gems/jewfish)
3
+
4
+ jewfish
5
+ =======
6
+
7
+ This software is originally published at https://github.com/unak/jewfish .
8
+
9
+
10
+ What's This?
11
+ ------------
12
+
13
+ A tiny website generator.
14
+
15
+
16
+ Requirements
17
+ ------------
18
+
19
+ * ruby 1.9.3 or newer
20
+ * kramdown
21
+
22
+
23
+ How to Use
24
+ ----------
25
+
26
+ (not written yet)
27
+
28
+
29
+ License
30
+ -------
31
+
32
+ Copyright (c) 2014 NAKAMURA Usaku usa@garbagecollect.jp
33
+
34
+ Redistribution and use in source and binary forms, with or without
35
+ modification, are permitted provided that the following conditions are met:
36
+
37
+ 1. Redistributions of source code must retain the above copyright notice,
38
+ this list of conditions and the following disclaimer.
39
+ 2. Redistributions in binary form must reproduce the above copyright notice,
40
+ this list of conditions and the following disclaimer in the documentation
41
+ and/or other materials provided with the distribution.
42
+
43
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
44
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
46
+ DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
47
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
48
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,19 @@
1
+ # -*- Ruby -*-
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask.rb"
4
+
5
+ desc "Start website"
6
+ task :start do
7
+ source = ENV["SRC"] || ENV["SOURCE"] || "sample"
8
+ ruby "-Ilib bin/jewfish start #{source}"
9
+ end
10
+
11
+ namespace "test" do
12
+ Rake::TestTask.new do |t|
13
+ t.name = "units"
14
+ t.pattern = "test/unit/test_*.rb"
15
+ end
16
+ end
17
+
18
+ desc "Run all tests"
19
+ task test: ["test:units"]
@@ -0,0 +1,4 @@
1
+ #!ruby
2
+ # -*- Ruby -*-
3
+ require "jewfish"
4
+ Jewfish.run(*ARGV)
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ # -*- Ruby -*-
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'jewfish/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "jewfish"
9
+ spec.version = Jewfish::VERSION
10
+ spec.authors = ["U.Nakamura"]
11
+ spec.email = ["usa@garbagecollect.jp"]
12
+ spec.summary = %q{tiny static website generator}
13
+ spec.description = %q{tiny static website generator}
14
+ spec.homepage = "https://github.com/unak/jewfish"
15
+ spec.license = "BSD-2-Clause"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "liquid"
23
+ spec.add_runtime_dependency "kramdown"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,7 @@
1
+ require "jewfish/command"
2
+
3
+ module Jewfish
4
+ def self.run(*args)
5
+ Command.run(*args)
6
+ end
7
+ end
@@ -0,0 +1,83 @@
1
+ module Jewfish
2
+ class Command
3
+ @@synonims = {
4
+ run: :start
5
+ }
6
+
7
+ def self.run(*args)
8
+ cmd = args.shift || 'help'
9
+ cmd = @@synonims[cmd.to_sym].to_s if @@synonims[cmd.to_sym]
10
+ lib = File.join(File.dirname(__FILE__), 'commands', cmd + '.rb')
11
+ raise "unknown subcommand: #{cmd}" unless File.exist?(lib)
12
+ require lib
13
+ mod = Jewfish.const_get(cmd.capitalize)
14
+ if args.size < mod.req || (!mod.rest && args.size > mod.req + mod.opt)
15
+ $stderr.puts "jewfish: not enough or too many parameters."
16
+ $stderr.puts
17
+ mod.show_usage($stderr)
18
+ exit 1
19
+ end
20
+
21
+ mod.new(*args)
22
+ end
23
+
24
+ def self.show_desc(io = $stdout)
25
+ io.puts "#{synonims}: #{@desc}"
26
+ end
27
+
28
+ def self.show_longdesc(io = $stdout)
29
+ return unless @longdesc
30
+ io.puts
31
+ io.puts @longdesc
32
+ end
33
+
34
+ def self.show_usage(io = $stdout)
35
+ io.puts "Usage: jewfish #{@usage}"
36
+ end
37
+
38
+ def self.req
39
+ reqs = instance_method(:initialize).parameters.find{|e| e.first == :req}
40
+ reqs ? reqs.size - 1 : 0
41
+ end
42
+
43
+ def self.opt
44
+ opts = instance_method(:initialize).parameters.find{|e| e.first == :opt}
45
+ opts ? opts.size - 1 : 0
46
+ end
47
+
48
+ def self.rest
49
+ instance_method(:initialize).parameters.find{|e| e.first == :rest}
50
+ end
51
+
52
+ def self.synonims
53
+ name = self.name.to_s.sub(/.*::/, '').downcase
54
+ syns = @@synonims.select{|k, v| v.to_s == name}.map{|k, v| k.to_s}
55
+ name << " (#{syns.join(', ')})" unless syns.empty?
56
+ name
57
+ end
58
+
59
+ private
60
+ def self.desc(str)
61
+ @desc = str
62
+ end
63
+
64
+ def self.longdesc(str)
65
+ @longdesc = str
66
+ end
67
+
68
+ def self.usage(str)
69
+ @usage = str
70
+ end
71
+
72
+ def parse_options(args)
73
+ opts = {}
74
+ args.each do |arg|
75
+ unless /\A--(.*?)(?:(?:\s+|=)(.*))?$/ =~ arg
76
+ raise "Invalid parameter: #{arg}"
77
+ end
78
+ opts[$1.to_sym] = $2 || true
79
+ end
80
+ opts
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,38 @@
1
+ require "jewfish/command"
2
+
3
+ module Jewfish
4
+ class Help < Command
5
+ desc "Show the help of jewfish."
6
+ usage "help [subcommand]"
7
+
8
+ def initialize(subcmd = nil)
9
+ if subcmd
10
+ subcmd = @@synonims[subcmd.to_sym].to_s if @@synonims[subcmd.to_sym]
11
+ lib = File.join(File.dirname(__FILE__), subcmd + ".rb")
12
+ unless File.exist?(lib)
13
+ $stderr.puts "#{subcmd}: unknown subcommand."
14
+ exit 1
15
+ end
16
+ require lib
17
+ mod = Jewfish.const_get(subcmd.capitalize)
18
+ mod.show_desc
19
+ puts
20
+ mod.show_usage
21
+ mod.show_longdesc
22
+ else
23
+ puts "Usage: jewfish <subcommand> [<parameters>]"
24
+ puts
25
+ puts "If you want to read the help of a subcommand, type 'jewfish help <subcommand>'."
26
+ puts
27
+ puts "Avairable subcommands:"
28
+
29
+ Dir.glob(File.join(File.dirname(__FILE__), '*.rb')).sort.each do |lib|
30
+ require lib
31
+ cmd = File.basename(lib, '.rb')
32
+ mod = Jewfish.const_get(cmd.capitalize)
33
+ puts " #{mod.synonims}"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,72 @@
1
+ require "jewfish/command"
2
+ require "jewfish/page"
3
+ require "webrick"
4
+
5
+ module Jewfish
6
+ class Start < Command
7
+ desc "Start your webserver."
8
+ usage "start <source directory> [options]"
9
+ longdesc <<-EOD
10
+ options:
11
+ --Port=<port> listen port (default: 3000)
12
+ --BindAddress=<address> bind address (default: 0.0.0.0)
13
+ --MaxClients=<num> num of max clients (default: 4)
14
+ --RequestTimeout=<sec> request timeout (default: 30)
15
+ --DoNotReverseLookup do not reverse lookup (default: false)
16
+ EOD
17
+
18
+ def initialize(srcdir, *opts)
19
+ tmp = parse_options(opts)
20
+ opts = {}
21
+ opts[:Port] = (tmp.delete(:Port) || 3000).to_i
22
+ opts[:BindAddress] = tmp.delete(:BindAddress) || "0.0.0.0"
23
+ opts[:MaxClients] = (tmp.delete(:MaxClients) || 4).to_i
24
+ opts[:RequestTimeout] = (tmp.delete(:RequestTimeout) || 30).to_i
25
+ opts[:DoNotReverseLookup] = tmp.delete(:DoNotReverseLookup)
26
+ raise "Invalid parameter(s): #{tmp.keys.join(', ')}" unless tmp.empty?
27
+
28
+ server = WEBrick::HTTPServer.new(opts)
29
+
30
+ shut = proc {server.shutdown}
31
+ siglist = %w"TERM QUIT"
32
+ siglist << %w"HUP INT" if $stdin.tty?
33
+ siglist &= Signal.list.keys
34
+ siglist.each do |sig|
35
+ Signal.trap(sig, shut)
36
+ end
37
+
38
+ server.mount_proc('/') do |req, res|
39
+ path = req.path.dup
40
+ path = File.join(path, 'index.html') if File.directory?(File.join(srcdir, path))
41
+ if File.exist?(src = File.join(srcdir, path)) ||
42
+ File.exist?(src = File.join(srcdir, File.dirname(path), File.basename(path, '.*') + '.md')) ||
43
+ File.exist?(src = File.join(srcdir, File.dirname(path), File.basename(path, '.*') + '.md.erb')) ||
44
+ File.exist?(src = File.join(srcdir, File.dirname(path), File.basename(path) + '.erb')) ||
45
+ File.exist?(src = File.join(srcdir, File.dirname(path), '_posts', File.basename(path, '.*') + '.md'))
46
+ res.body = Page.convert(src, path)
47
+ elsif File.directory?(File.join(srcdir, req.path, '_posts'))
48
+ page = Page.new(nil, path)
49
+ page.src = File.join(srcdir, File.dirname(path), 'index.md.erb')
50
+ page.params['layout'] = 'page'
51
+ page.params['title'] = File.basename(req.path).capitalize
52
+ page.content = <<-EOC
53
+ # <%= @title %>
54
+ <% Dir.glob(File.join(@dir, '_posts', '*.md')).sort_by{|e| -File.mtime(e).to_i}.each do |md| %>
55
+ <% entry = Jewfish::Page.new(md, File.join(File.dirname(@path), File.basename(md, '.md') + '.html')) %>
56
+ * [<%= entry['title'] %>](<%= entry.path %>)
57
+ <% end %>
58
+ EOC
59
+ res.body = page.convert
60
+ else
61
+ raise WEBrick::HTTPStatus::NotFound, path
62
+ end
63
+ res.content_type = WEBrick::HTTPUtils.mime_type(path, WEBrick::HTTPUtils::DefaultMimeTypes)
64
+ if res.content_type == "text/html"
65
+ res.content_type << "; charset=utf-8"
66
+ end
67
+ end
68
+
69
+ server.start
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,77 @@
1
+ require "erb"
2
+ require "kramdown"
3
+ require "yaml"
4
+
5
+ module Jewfish
6
+ class Page
7
+ def self.convert(src, path, content: nil, params: {})
8
+ page = Page.new(src, path)
9
+ page.convert(content, params)
10
+ end
11
+
12
+ attr_accessor :src, :path, :content, :plain, :params
13
+
14
+ def initialize(src, path)
15
+ @src = src.dup if src
16
+ @path = path.dup
17
+ @content = @plain = File.binread(src) if src
18
+ if /\A---\r?\n/ =~ @plain
19
+ @params = YAML.load(@plain)
20
+ @content = @plain.sub(/\A---$.*?^---$\s*/m, '')
21
+ else
22
+ @params = {}
23
+ end
24
+ end
25
+
26
+ def to_s
27
+ @content
28
+ end
29
+
30
+ def [](key)
31
+ @params[key]
32
+ end
33
+
34
+ def convert(content = nil, params = {})
35
+ erb(content, params) if /\.erb\z/ =~ src
36
+ convert_md if /\.md\b/ =~ src
37
+ apply_layout
38
+ to_s
39
+ end
40
+
41
+ def convert_md
42
+ @content = Kramdown::Document.new(@content.force_encoding('utf-8')).to_html
43
+ end
44
+
45
+ def erb(content = nil, params = {})
46
+ params = @params.merge(params)
47
+ erb = ERB.new(@content, nil, '<>', @src.gsub(/\W/, '_'))
48
+ mod = erb.def_module
49
+ obj = Object.new
50
+ obj.extend(mod)
51
+ params.each_pair do |k, v|
52
+ obj.instance_variable_set("@#{k}".to_sym, v)
53
+ end
54
+ obj.instance_variable_set(:@dir, File.dirname(@src))
55
+ obj.instance_variable_set(:@path, @path)
56
+ @content = obj.erb{content}
57
+ end
58
+
59
+ def apply_layout
60
+ if @params["layout"] && @params["layout"] != "nil"
61
+ dir = File.dirname(@src)
62
+ applied = false
63
+ while dir != File.dirname(dir)
64
+ if File.directory?(File.join(dir, "_layouts"))
65
+ if File.exist?(layout = File.join(dir, "_layouts", @params["layout"] + ".html.erb"))
66
+ @content = Page.convert(layout, @path, content: @content, params: @params)
67
+ applied = true
68
+ break
69
+ end
70
+ end
71
+ dir = File.dirname(dir)
72
+ end
73
+ warn "layout '#{@params['layout']}' is not found" unless applied
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module Jewfish
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title><%= @title %></title>
5
+ </head>
6
+ <body>
7
+ <%= yield %>
8
+ </body>
9
+ </html>
@@ -0,0 +1,8 @@
1
+ ---
2
+ layout: page
3
+ title: Sample Website
4
+ ---
5
+
6
+ <h1>Sample Website</h1>
7
+ <p>
8
+ <a href="news">News</a>
@@ -0,0 +1,9 @@
1
+ ---
2
+ layout: page
3
+ title: Sample 1
4
+ ---
5
+
6
+ sample1
7
+ =======
8
+
9
+ this is a sample article 1.
@@ -0,0 +1,9 @@
1
+ ---
2
+ layout: page
3
+ title: Sample 2
4
+ ---
5
+
6
+ sample2
7
+ =======
8
+
9
+ this is a sample article 2.
@@ -0,0 +1,9 @@
1
+ ---
2
+ layout: page
3
+ title: Sample 3
4
+ ---
5
+
6
+ sample3
7
+ =======
8
+
9
+ this is a sample article 3.
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jewfish
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - U.Nakamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: liquid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: kramdown
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: tiny static website generator
70
+ email:
71
+ - usa@garbagecollect.jp
72
+ executables:
73
+ - jewfish
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/jewfish
83
+ - jewfish.gemspec
84
+ - lib/jewfish.rb
85
+ - lib/jewfish/command.rb
86
+ - lib/jewfish/commands/help.rb
87
+ - lib/jewfish/commands/start.rb
88
+ - lib/jewfish/page.rb
89
+ - lib/jewfish/version.rb
90
+ - sample/_layouts/page.html.erb
91
+ - sample/index.html
92
+ - sample/news/_posts/sample1.md
93
+ - sample/news/_posts/sample2.md
94
+ - sample/news/_posts/sample3.md
95
+ homepage: https://github.com/unak/jewfish
96
+ licenses:
97
+ - BSD-2-Clause
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.2.2
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: tiny static website generator
119
+ test_files: []