kdbook 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/kdbook +158 -0
  3. metadata +130 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b662b7d1f0e04d742dd2c5f37985e9a3fd46e4eb
4
+ data.tar.gz: e41ddc9fcd127efb7a080f858afa5a6e91854ede
5
+ SHA512:
6
+ metadata.gz: 65d277d1a0557635a7005da31c50b23e9562a5381c64f6e16009265a8c814a8d5ada532c1cb97231a7f37bb1412d40b44c09e9a6edefe7047ee34b0385d566e0
7
+ data.tar.gz: fce4f9f047fa4b2954701d008140f35d650396da71a15677ee379b0918871fbd48602ebbbc17d34a53714aa01633f31ced75a1e2f1a7fdd69c6b5a291c0460b7
data/bin/kdbook ADDED
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'thor'
4
+ require 'yaml'
5
+ require 'colorize'
6
+ require 'kramdown'
7
+ require 'recursive-open-struct'
8
+
9
+ VERSION = '0.0.1'
10
+
11
+ class KdbookBin < Thor
12
+ class_option :'config-file', type: :string, aliases: 'f', default: 'kdbook.yml'
13
+
14
+ desc 'wordcount', 'Count the number of words in the book'
15
+ long_desc <<-LONGDESC
16
+ Count the number of words in the book. If a goal is specified in
17
+ the project's yml file, will also print percentage complete.
18
+ LONGDESC
19
+ def wordcount
20
+ file_list(options).each do |inp|
21
+ if inp.wordcount
22
+ wc, file = %x(wc -w #{inp.input}).chomp.split
23
+ output = "Wordcount of #{file} is #{add_commas(wc)}"
24
+ if !inp.wordcount.is_a?(String) && inp.wordcount.min
25
+ output = "#{output}. #{percentify(wc, inp.wordcount.min)}% " \
26
+ "of goal #{inp.wordcount.min}"
27
+ output = "#{output} to #{inp.wordcount.max}" if inp.wordcount.max
28
+ end
29
+ puts output.green
30
+ end
31
+ end
32
+ end
33
+
34
+ desc 'version', 'Check current installed version of kdbook'
35
+ def version
36
+ puts "Kdbook - Version: #{VERSION}"
37
+ end
38
+
39
+ desc 'build', 'Build PDF and HTML versions of the input files'
40
+ long_desc <<-LONGDESC
41
+ If specific formats are specified in the config file, those
42
+ will be built.
43
+
44
+ You can optionally pass in filenames as args to build specific files
45
+ LONGDESC
46
+ def build(*files)
47
+ file_list(options).each do |file|
48
+ file.output.each do |out|
49
+ case out
50
+ when /pdf/i
51
+ to_pdf(file.input, output_file(file.input, out))
52
+ when /html/i
53
+ to_html(file.input, output_file(file.input, out), file.template)
54
+ else
55
+ puts "Output format '#{out}' is unsupported!".red
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ desc 'clean', 'Delete build artifacts'
62
+ def clean
63
+ output_file_list(options).each do |f|
64
+ puts "Cleaning file '#{f}'".blue
65
+ File.delete(f) if File.exists?(f)
66
+ puts "Failed to remove file '#{f}'" if File.exists?(f)
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def file_list(options)
73
+ config(options)
74
+ end
75
+
76
+ def input_file_list(options)
77
+ file_list(options).map(&:input)
78
+ end
79
+
80
+ def output_file(input_file, out_format)
81
+ input_file.gsub(/md$/, out_format)
82
+ end
83
+
84
+ def output_files(input_file, out_formats)
85
+ out_formats.map{ |out| output_file(input_file, out) }
86
+ end
87
+
88
+ def output_file_list(options)
89
+ file_list(options).map{ |file| output_files(file.input, file.output) }.flatten!
90
+ end
91
+
92
+ def rm_hidden_comments(input)
93
+ input.gsub(/\/--.*?--\//m, "")
94
+ end
95
+
96
+ def read_file(file)
97
+ rm_hidden_comments(File.read(file))
98
+ end
99
+
100
+ def to_pdf(inp, out)
101
+ puts "Generating #{out}".blue
102
+ k = Kramdown::Document.new(read_file(inp))
103
+ File.write(out, k.to_pdf)
104
+ end
105
+
106
+ def to_html(inp, out, template = nil)
107
+ puts "Generating #{out}".blue
108
+ k = if template
109
+ Kramdown::Document.new(read_file(inp), template: template)
110
+ else
111
+ Kramdown::Document.new(read_file(inp))
112
+ end
113
+ File.write(out, k.to_html)
114
+ end
115
+
116
+ def add_commas(number)
117
+ number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
118
+ end
119
+
120
+ def percentify(num, den)
121
+ ((num.to_f / den.to_f) * 100.0).round(2)
122
+ end
123
+
124
+ def config_file_contents(options)
125
+ if File.exists?(options[:'config-file'])
126
+ YAML.load_file(options[:'config-file'])['kdbook']
127
+ else
128
+ # pass a straight array of filenames in the current dir name *.md
129
+ Dir.glob('*.md')
130
+ end
131
+ end
132
+
133
+ def upgrade_file_string(file)
134
+ file.is_a?(String) ? { input: file } : file
135
+ end
136
+
137
+ def add_missing_output(file)
138
+ file[:output] = %w[html pdf] unless file[:output]
139
+ file
140
+ end
141
+
142
+ def config(options)
143
+ config_file_contents(options)
144
+ .map { |f| upgrade_file_string(f) }
145
+ .map { |f| add_missing_output(f) }
146
+ .map { |f| RecursiveOpenStruct.new(f, recurse_over_arrays: true) }
147
+ end
148
+ end
149
+
150
+ aliases = {
151
+ 'wc' => 'wordcount'
152
+ }
153
+
154
+ if !ARGV.empty? && %w[-v --version].include?(ARGV.first)
155
+ puts "Kdbook - Version: #{VERSION}"
156
+ else
157
+ KdbookBin.start(ARGV.map { |a| aliases.keys.include?(a) ? aliases[a] : a })
158
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kdbook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Porter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.19'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.19'
27
+ - !ruby/object:Gem::Dependency
28
+ name: kramdown
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: prawn
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: prawn-table
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: recursive-open-struct
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ description: book writing in markdown made easy
98
+ email: BenjaminPorter86@gmail.com
99
+ executables:
100
+ - kdbook
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - bin/kdbook
105
+ homepage: https://github.com/FreedomBen/kdbook
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.5
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: book writing in markdown made easy
129
+ test_files: []
130
+ has_rdoc: