bookpress 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
+ SHA1:
3
+ metadata.gz: 838b7c0ad423f9052e260660e967859b06bc51a4
4
+ data.tar.gz: d59104a31c13e535254ed279e7701352966edf31
5
+ SHA512:
6
+ metadata.gz: 2477e80cb2978b2938e99950505478552799a96c25806d87b94122492bf892ea8518254cbb6013f3f37a888f10ae2fe2b84ac92da6272eb0a61bd694cd9ec738
7
+ data.tar.gz: 37b472829eb00dd965fa38ba1e69221763a53e9c88bf1006536803bd1e671795a588de723432638c5718e01ffff26971737bd298c28f3e949b9f6b4e315c3d13
data/.gitignore ADDED
@@ -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
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bookpress.gemspec
4
+ gemspec
5
+
6
+ gem 'nokogiri'
7
+ gem 'redcarpet'
8
+ gem 'pygments.rb'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Matthew Sullivan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Bookpress
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bookpress'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bookpress
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/bookpress/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bookpress.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bookpress/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bookpress"
8
+ spec.version = Bookpress::VERSION
9
+ spec.authors = ["Matthew Sullivan"]
10
+ spec.email = ["msull92@gmail.com"]
11
+ spec.summary = %q{Bookpress is a small gem that spits out a single HTML document, generated
12
+ from an implicit directory structure of markdown files.}
13
+ spec.homepage = "https://github.com/msull92/bookpress"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "nokogiri"
22
+ spec.add_dependency "redcarpet"
23
+ spec.add_dependency "pygments.rb"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,3 @@
1
+ module Bookpress
2
+ VERSION = "0.0.1"
3
+ end
data/lib/bookpress.rb ADDED
@@ -0,0 +1,147 @@
1
+ # require "bookpress/version"
2
+ require "bundler"
3
+ require "pathname"
4
+
5
+ Bundler.require(:default)
6
+
7
+ # This reopens the Hash class to add a method to allow sorting by key recursively
8
+ class Hash
9
+ def sort_by_key(recursive = false, &block)
10
+ self.keys.sort(&block).reduce({}) do |seed, key|
11
+ seed[key] = self[key]
12
+ if recursive && seed[key].is_a?(Hash)
13
+ seed[key] = seed[key].sort_by_key(true, &block)
14
+ end
15
+ seed
16
+ end
17
+ end
18
+ end
19
+
20
+ module Bookpress
21
+ class Book
22
+ attr_accessor :title, :pages, :tree
23
+
24
+ def initialize(directory)
25
+ # Get all markdown files
26
+ @pages = Pathname.glob("#{directory}/" "**/*.{md,markdown}")
27
+
28
+ # Create a Hash to store the book tree structure
29
+ @tree = Hash.new { |h, k| h[k] = Hash.new &h.default_proc }
30
+
31
+ @pages.each do |page|
32
+ subdirectory = @tree
33
+ page_name = page.basename
34
+ page.cleanpath.each_filename do |segment|
35
+ subdirectory[segment]
36
+ if segment.to_s == page_name.to_s
37
+ subdirectory[segment] = Utility.markdown_renderer.render(page.read)
38
+ end
39
+ subdirectory = subdirectory[segment]
40
+ end
41
+ end
42
+
43
+ # Sort the structure by key
44
+ @tree = @tree.sort_by_key(true) { |x, y| Utility.orderify(x.to_s) <=> Utility.orderify(y.to_s) }
45
+
46
+ # Build the document
47
+ builder = Nokogiri::HTML::Builder.new do |doc|
48
+ doc.html do
49
+ doc.body do
50
+ doc.cdata Utility.articlize(@tree)
51
+ end
52
+ end
53
+ end
54
+
55
+ filename = "#{@tree.first.first}.html"
56
+ File.open(filename, 'w') { |file| file.truncate(0) }
57
+ File.write(filename, builder.to_html)
58
+ end
59
+ end
60
+
61
+ class Utility
62
+ def self.titleize(title)
63
+ if title
64
+ new_title = title.sub(/(\d*_)/, '')
65
+ really_new_title = new_title.sub(/(\.\w*)/, '')
66
+ words = really_new_title.to_s.split('_')
67
+ words.each do |word|
68
+ if word.length <3
69
+ word.downcase!
70
+ elsif word.length >3
71
+ word.capitalize!
72
+ end
73
+ end
74
+ words.join ' '
75
+ else
76
+ ''
77
+ end
78
+ end
79
+
80
+ def self.idify(title)
81
+ if title
82
+ new_title = title.sub(/(\d*_)/, '')
83
+ really_new_title = new_title.sub(/(\.\w*)/, '')
84
+ extremely_new_title = really_new_title.sub(/ /, '_')
85
+ extremely_new_title.downcase!
86
+ extremely_new_title
87
+ else
88
+ ''
89
+ end
90
+ end
91
+
92
+ def self.orderify(title)
93
+ if title
94
+ /(\d*)_/.match(title)[1]
95
+ else
96
+ ''
97
+ end
98
+ end
99
+
100
+ def self.articlize(tree, parent = nil)
101
+ unless tree.is_a?(String)
102
+ article = ""
103
+ tree.each do |key, value|
104
+ html = ""
105
+ if Utility.idify(key) == Utility.idify(parent)
106
+ if value.is_a?(String)
107
+ html << value
108
+ else
109
+ html << Utility.articlize(tree[key], key)
110
+ end
111
+ else
112
+ html = "<article id='#{Utility.idify(key)}'>"
113
+ html << ("<header><h1>#{Utility.titleize(key)}</h1></header>")
114
+ if value.is_a?(String)
115
+ html << value
116
+ else
117
+ html << Utility.articlize(tree[key], key)
118
+ end
119
+ html << "</article>"
120
+ end
121
+ article << html
122
+ end
123
+ article
124
+ else
125
+ tree
126
+ end
127
+ end
128
+
129
+ def self.markdown_renderer
130
+ Redcarpet::Markdown.new(Bookpress::HTMLRenderer, {
131
+ autolink: true,
132
+ disable_indented_code_blocks: true,
133
+ fenced_code_blocks: true,
134
+ space_after_headers: true
135
+ })
136
+ end
137
+ end
138
+
139
+ class HTMLRenderer < Redcarpet::Render::HTML
140
+ include Redcarpet::Render::SmartyPants
141
+
142
+ def block_code(code, language)
143
+ result = Pygments.highlight(code, lexer: language)
144
+ result
145
+ end
146
+ end
147
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bookpress
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Sullivan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
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: redcarpet
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: pygments.rb
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - msull92@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - bookpress.gemspec
96
+ - lib/bookpress.rb
97
+ - lib/bookpress/version.rb
98
+ homepage: https://github.com/msull92/bookpress
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.2.2
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Bookpress is a small gem that spits out a single HTML document, generated
122
+ from an implicit directory structure of markdown files.
123
+ test_files: []