makimono 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7b7bafe35f02667e8774cbc02eb7633aac4daacd7ab7432e2361d60f0751cb0b
4
+ data.tar.gz: f6919cd44e92c0b981b8dcbd2f13759b4c3400de29fd1f6930444875d8b0db62
5
+ SHA512:
6
+ metadata.gz: 40c5bbae200a1a427992900c306db816e9e7c7f89ca0503bde06d499dccaccffcf6fcc71291e25ef7dfd997e9fa42cf5f032149193a7bf60640182b4c587197f
7
+ data.tar.gz: a3e9dea655f13fa16defcaacbca7e4c80e7d2c670f0b6ae4e1db7ee2a7a6557c266c39b722e707e0a07b8e9c025fcba601bffc3f456e8c2ee1ccfc54675daf17
@@ -0,0 +1,33 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+ branches:
9
+ - master
10
+
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-latest
14
+
15
+ strategy:
16
+ matrix:
17
+ ruby:
18
+ - 2.6
19
+ - 2.7
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+
24
+ - name: Set up Ruby
25
+ uses: ruby/setup-ruby@v1
26
+ with:
27
+ ruby-version: ${{ matrix.ruby }}
28
+
29
+ - name: Install dependencies
30
+ run: bundle install
31
+
32
+ - name: Run tests
33
+ run: bundle exec rake
@@ -0,0 +1,19 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ .ruby-version
11
+ Gemfile.lock
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
15
+
16
+ # fixtures
17
+ /spec/fixtures/out/*
18
+ !/spec/fixtures/out/
19
+ !/spec/fixtures/out/.keep
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,28 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ NewCops: enable
6
+
7
+ # rubocop
8
+
9
+ Metrics/BlockLength:
10
+ Exclude:
11
+ - spec/**/*
12
+
13
+ Style/ClassAndModuleChildren:
14
+ Enabled: false
15
+
16
+ Style/Documentation:
17
+ Enabled: false
18
+
19
+ # rubocop-rspec
20
+
21
+ RSpec/ExampleLength:
22
+ Enabled: false
23
+
24
+ RSpec/MultipleExpectations:
25
+ Enabled: false
26
+
27
+ RSpec/VerifiedDoubles:
28
+ IgnoreSymbolicNames: true
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## Unreleased
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at fujinakahara2032@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in makimono.gemspec
6
+ gemspec
7
+
8
+ gem 'aruba'
9
+ gem 'rake'
10
+ gem 'rspec'
11
+ gem 'rubocop'
12
+ gem 'rubocop-rspec'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Fuji Nakahara
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Makimono
2
+
3
+ Makimono is a simple ebook generator.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'makimono'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install makimono
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ 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.
28
+
29
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fuji-nakahara/makimono. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/fuji-nakahara/makimono/blob/master/CODE_OF_CONDUCT.md).
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
38
+
39
+ ## Code of Conduct
40
+
41
+ Everyone interacting in the Makimono project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fuji-nakahara/makimono/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'makimono'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'makimono/cli'
5
+
6
+ if File.file?('Gemfile')
7
+ require 'bundler'
8
+ Bundler.setup
9
+ Bundler.require(:makimono)
10
+ end
11
+
12
+ Makimono::CLI.start
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'makimono/configuration'
4
+ require_relative 'makimono/error'
5
+ require_relative 'makimono/processor'
6
+ require_relative 'makimono/version'
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+
5
+ require_relative '../makimono'
6
+ require_relative 'command/build'
7
+ require_relative 'command/new'
8
+
9
+ module Makimono
10
+ class CLI < Thor
11
+ def self.exit_on_failure?
12
+ true
13
+ end
14
+
15
+ option :config, aliases: :c, banner: 'FILE', default: 'makimono.yml'
16
+ register Command::Build, 'build', 'build', 'Build an ebook'
17
+
18
+ register Command::New, 'new', 'new NAME', 'Create a new Makimono project'
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module Makimono
6
+ module Command
7
+ class Build < Thor::Group
8
+ class_option :config, aliases: :c, default: 'makimono.yml'
9
+
10
+ def load_user_config
11
+ user_config = if File.file?(options[:config])
12
+ yaml = File.read(options[:config])
13
+ YAML.safe_load(yaml, filename: File.basename(options[:config]), symbolize_names: true)
14
+ else
15
+ {}
16
+ end
17
+ @config = Configuration.new(user_config)
18
+ end
19
+
20
+ def load_user_library
21
+ Dir[File.join(@config[:library], '**/*.rb')].sort.each { |f| require f } if Dir.exist?(@config[:library])
22
+ end
23
+
24
+ def process
25
+ Makimono::Processor.new(@config).process
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+
5
+ module Makimono
6
+ module Command
7
+ class New < Thor::Group
8
+ include Thor::Actions
9
+
10
+ argument :name, required: true
11
+
12
+ def self.source_root
13
+ File.expand_path('../../project_template', __dir__)
14
+ end
15
+
16
+ def set_destination_root
17
+ self.destination_root = name
18
+ end
19
+
20
+ def create_files
21
+ copy_file('.gitignore')
22
+ copy_file('Gemfile')
23
+ template('makimono.yml', { name: name })
24
+ copy_file('src/01-Title.md', "src/01-#{name.capitalize}.md")
25
+ create_file('out/.keep')
26
+ end
27
+
28
+ def run_bundle_install
29
+ in_root do
30
+ run('bundle install')
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'securerandom'
5
+
6
+ require_relative 'style'
7
+
8
+ module Makimono
9
+ class Configuration
10
+ extend Forwardable
11
+
12
+ DEFAULTS = {
13
+ # Makimono
14
+ source: File.expand_path('src'),
15
+ output: File.expand_path('out'),
16
+ style: nil,
17
+ template: 'default.xhtml',
18
+ markdown: 'CommonMarker',
19
+ converters: %w[Markdown],
20
+ generator: 'EPUB',
21
+ ebook_file_name: 'book',
22
+ library: File.expand_path('lib'),
23
+
24
+ # EPUB
25
+ identifier: "urn:uuid:#{SecureRandom.uuid}",
26
+ title: 'No title',
27
+ language: 'ja'
28
+ }.freeze
29
+
30
+ def_delegators :@config, :[], :fetch, :dig, :slice
31
+
32
+ def initialize(config = {})
33
+ @config = DEFAULTS.merge(config)
34
+ end
35
+
36
+ def to_h
37
+ @config
38
+ end
39
+
40
+ def style
41
+ return @style if defined? @style
42
+
43
+ @style = if @config[:style].nil? || @config[:style].empty?
44
+ nil
45
+ else
46
+ Style.from_style_config(@config[:style])
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Makimono
4
+ class Converter
5
+ autoload :Markdown, 'makimono/converter/markdown'
6
+
7
+ def self.from_config(config)
8
+ klasses = if config[:converters] == ['Markdown']
9
+ [Markdown]
10
+ else
11
+ config[:converters].map do |name|
12
+ const_get(name.to_s)
13
+ rescue NameError
14
+ raise InvalidConverterError, "Invalid converter configuration: #{name}"
15
+ end
16
+ end
17
+ new(klasses.map { |klass| klass.new(config) })
18
+ end
19
+
20
+ attr_reader :converters
21
+
22
+ def initialize(converters)
23
+ @converters = converters
24
+ end
25
+
26
+ def convert(resource)
27
+ converters.inject(resource) do |res, converter|
28
+ if converter.convertible?(res)
29
+ converter.convert(res)
30
+ else
31
+ res
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+
5
+ module Makimono
6
+ class Converter
7
+ class Markdown
8
+ autoload :CommonMarker, 'makimono/converter/markdown/commonmarker'
9
+ autoload :FujiMarkdown, 'makimono/converter/markdown/fuji_markdown'
10
+
11
+ PRESETS = %w[default.xhtml empty.txt].freeze
12
+
13
+ def self.get_markdown_class(class_name)
14
+ case class_name
15
+ when 'CommonMarker'
16
+ CommonMarker
17
+ when 'FujiMarkdown'
18
+ FujiMarkdown
19
+ else
20
+ const_get(class_name.to_s)
21
+ end
22
+ rescue NameError
23
+ raise InvalidMarkdownError, "Invalid markdown configuration: #{class_name}"
24
+ end
25
+
26
+ def self.get_template_path(template_config)
27
+ if PRESETS.include?(template_config)
28
+ File.expand_path("../../templates/#{template_config}.erb", __dir__)
29
+ elsif File.file?(template_config.to_s)
30
+ File.expand_path(template_config.to_s)
31
+ else
32
+ raise InvalidTemplateError, "Template file `#{template_config}` does not exist"
33
+ end
34
+ end
35
+
36
+ def initialize(config, markdown: nil)
37
+ @config = config
38
+ @markdown = markdown || self.class.get_markdown_class(config[:markdown]).new(config)
39
+ @template_path = self.class.get_template_path(config[:template])
40
+ end
41
+
42
+ def convertible?(resource)
43
+ %w[.markdown .mkdown .mkdn .mkd .md].include?(resource.extname)
44
+ end
45
+
46
+ def convert(resource)
47
+ converted = resource.dup
48
+ converted.content = convert_content(resource)
49
+ converted.extname = File.extname(File.basename(@template_path, '.erb'))
50
+ converted
51
+ end
52
+
53
+ private
54
+
55
+ def convert_content(resource)
56
+ body = @markdown.render(resource.content)
57
+ ERB.new(template).result_with_hash(
58
+ {
59
+ config: @config,
60
+ resource: resource,
61
+ body: body
62
+ }
63
+ )
64
+ end
65
+
66
+ def template
67
+ @template ||= File.read(@template_path)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'commonmarker'
4
+
5
+ module Makimono
6
+ class Converter
7
+ class Markdown
8
+ class CommonMarker
9
+ def initialize(config)
10
+ @config = config
11
+ end
12
+
13
+ def render(markdown)
14
+ ::CommonMarker.render_html(markdown, options, extensions)
15
+ end
16
+
17
+ private
18
+
19
+ def options
20
+ @config.dig(:CommonMarker, :options)&.map(&:upcase)&.map(&:to_sym) || :DEFAULT
21
+ end
22
+
23
+ def extensions
24
+ @config.dig(:CommonMarker, :extensions)&.map(&:downcase)&.map(&:to_sym) || []
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fuji_markdown'
4
+
5
+ module Makimono
6
+ class Converter
7
+ class Markdown
8
+ class FujiMarkdown
9
+ def initialize(config)
10
+ @config = config
11
+ end
12
+
13
+ def render(markdown)
14
+ ::FujiMarkdown.render(markdown, option)
15
+ end
16
+
17
+ private
18
+
19
+ def option
20
+ @config[:FujiMarkdown]&.upcase&.to_sym || :HTML
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Makimono
4
+ Error = Class.new(StandardError)
5
+ InvalidConfigurationError = Class.new(Error)
6
+ InvalidSourceError = Class.new(InvalidConfigurationError)
7
+ InvalidConverterError = Class.new(InvalidConfigurationError)
8
+ InvalidGeneratorError = Class.new(InvalidConfigurationError)
9
+ InvalidMarkdownError = Class.new(InvalidConfigurationError)
10
+ InvalidTemplateError = Class.new(InvalidConfigurationError)
11
+ InvalidStyleError = Class.new(InvalidConfigurationError)
12
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Makimono
4
+ class Generator
5
+ autoload :Epub, 'makimono/generator/epub'
6
+ autoload :SimpleFile, 'makimono/generator/simple_file'
7
+
8
+ def self.from_config(config)
9
+ klass = if config[:generator].upcase == 'EPUB'
10
+ Epub
11
+ elsif config[:generator] == 'SimpleFile'
12
+ SimpleFile
13
+ else
14
+ const_get(config[:generator].to_s)
15
+ end
16
+ new(klass.new(config))
17
+ rescue NameError
18
+ raise InvalidGeneratorError, "Invalid generator configuration: #{config[:generator]}"
19
+ end
20
+
21
+ attr_reader :generator
22
+
23
+ def initialize(generator)
24
+ @generator = generator
25
+ end
26
+
27
+ def generate(documents)
28
+ generator.generate(documents)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'gepub'
4
+ require 'time'
5
+
6
+ module Makimono
7
+ class Generator
8
+ class Epub
9
+ def initialize(config, book: GEPUB::Book.new)
10
+ @config = config
11
+ @book = book
12
+ end
13
+
14
+ def generate(resources)
15
+ add_required_metadata
16
+ add_optional_metadata
17
+ @book.page_progression_direction = @config[:page_progression_direction] if @config[:page_progression_direction]
18
+
19
+ add_items([@config.style, *resources.reject(&:ordered?)].compact)
20
+ add_ordered_items(resources.select(&:ordered?).sort_by(&:number))
21
+
22
+ @book.generate_epub("#{File.expand_path(@config[:ebook_file_name], @config[:output])}.epub")
23
+ end
24
+
25
+ private
26
+
27
+ def add_required_metadata
28
+ @book.identifier = @config.fetch(:identifier)
29
+ @book.title = @config.fetch(:title)
30
+ @book.language = @config.fetch(:language)
31
+ @book.lastmodified = parse_config_time(:modified) if @config[:modified]
32
+ rescue KeyError => e
33
+ raise InvalidConfigurationError, "Required metadata not found: #{e.key}"
34
+ end
35
+
36
+ def add_optional_metadata
37
+ Array(@config[:creator]).each do |creator|
38
+ @book.add_creator(creator)
39
+ end
40
+ Array(@config[:contributor]).each do |contributor|
41
+ @book.add_contributor(contributor)
42
+ end
43
+
44
+ @book.add_date(parse_config_time(:date).iso8601) if @config[:date]
45
+ end
46
+
47
+ def add_items(resources)
48
+ resources.each do |resource|
49
+ @book.add_item(resource.path)
50
+ .add_raw_content(resource.content)
51
+ end
52
+ end
53
+
54
+ def add_ordered_items(resources)
55
+ @book.ordered do
56
+ resources.each do |resource|
57
+ @book.add_item(resource.path)
58
+ .add_raw_content(resource.content)
59
+ .toc_text(resource.title)
60
+ end
61
+ end
62
+ end
63
+
64
+ def parse_config_time(key)
65
+ Time.parse(@config[key].to_s)
66
+ rescue ArgumentError
67
+ raise InvalidConfigurationError, "Invalid time format: #{key}"
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Makimono
4
+ class Generator
5
+ class SimpleFile
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+
10
+ def generate(resources)
11
+ FileUtils.mkdir_p(@config[:output]) unless Dir.exist?(@config[:output])
12
+
13
+ resources.each do |resource|
14
+ output_path = File.expand_path(resource.path, @config[:output])
15
+ File.write(output_path, resource.content)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'converter'
4
+ require_relative 'generator'
5
+ require_relative 'resource_list'
6
+
7
+ module Makimono
8
+ class Processor
9
+ def initialize(config, converter: Converter.from_config(config), generator: Generator.from_config(config))
10
+ @config = config
11
+ @converter = converter
12
+ @generator = generator
13
+ end
14
+
15
+ def process(resources = ResourceList.from_dir(@config[:source]))
16
+ converted_resources = resources.map do |resource|
17
+ @converter.convert(resource)
18
+ end
19
+ @generator.generate(converted_resources)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Makimono
4
+ class Resource
5
+ ORDERED_BASENAME_REGEXP = /\A(?<number>\d+)-(?<title>.+)\z/.freeze
6
+
7
+ attr_accessor :path, :content
8
+
9
+ def initialize(path, content)
10
+ @path = path
11
+ @content = content
12
+ end
13
+
14
+ def dirname
15
+ File.dirname(path)
16
+ end
17
+
18
+ def basename
19
+ File.basename(path, '.*')
20
+ end
21
+
22
+ def basename=(new_basename)
23
+ self.path = "#{dirname}/#{new_basename}#{extname}"
24
+ end
25
+
26
+ def extname
27
+ File.extname(path)
28
+ end
29
+
30
+ def extname=(new_extname)
31
+ self.path = "#{dirname}/#{basename}#{new_extname}"
32
+ end
33
+
34
+ def ordered?
35
+ basename.match?(ORDERED_BASENAME_REGEXP)
36
+ end
37
+
38
+ def number
39
+ basename[ORDERED_BASENAME_REGEXP, :number]&.to_i
40
+ end
41
+
42
+ def title
43
+ basename[ORDERED_BASENAME_REGEXP, :title]
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'resource'
4
+
5
+ module Makimono
6
+ class ResourceList
7
+ include Enumerable
8
+
9
+ def self.from_dir(dir)
10
+ raise InvalidSourceError, "Source directory `#{dir}` does not exist" unless Dir.exist?(dir)
11
+
12
+ resources = []
13
+ Dir.chdir(dir) do
14
+ Dir['**/*'].each do |path|
15
+ content = File.read(path)
16
+ resources << Resource.new(path, content)
17
+ end
18
+ end
19
+ new(resources)
20
+ end
21
+
22
+ def initialize(resources)
23
+ @resources = resources
24
+ end
25
+
26
+ def each(&block)
27
+ @resources.each(&block)
28
+ end
29
+
30
+ def ordered
31
+ @resources.select(&:ordered?).sort_by(&:number)
32
+ end
33
+
34
+ def not_ordered
35
+ @resources.reject(&:ordered?)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'resource'
4
+
5
+ module Makimono
6
+ class Style < Resource
7
+ PRESETS = %w[fuji fuji_tategaki].freeze
8
+
9
+ def self.from_style_config(style_config)
10
+ path = if PRESETS.include?(style_config)
11
+ File.expand_path("../styles/#{style_config}.css", __dir__)
12
+ elsif File.file?(style_config.to_s)
13
+ File.expand_path(style_config.to_s)
14
+ else
15
+ raise InvalidStyleError, "Style file `#{style_config}` does not exist"
16
+ end
17
+
18
+ new(File.basename(path), File.read(path))
19
+ end
20
+
21
+ def relative_path_from(dir)
22
+ Pathname.new(path).relative_path_from(dir)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Makimono
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,3 @@
1
+ /out/*
2
+ !/out/
3
+ !/out/.keep
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'makimono'
@@ -0,0 +1,3 @@
1
+ identifier: 'urn:uuid:<%= SecureRandom.uuid %>'
2
+ title: '<%= config[:name].capitalize %>'
3
+ ebook_file_name: '<%= config[:name] %>'
@@ -0,0 +1 @@
1
+ Write your text here!
@@ -0,0 +1,14 @@
1
+ p {
2
+ margin: 0;
3
+ }
4
+
5
+ hr {
6
+ border: 0;
7
+ margin: 1em 0;
8
+ }
9
+
10
+ em {
11
+ font-style: normal;
12
+ text-emphasis-style: filled sesame;
13
+ -webkit-text-emphasis-style: filled sesame;
14
+ }
@@ -0,0 +1,23 @@
1
+ body {
2
+ writing-mode: vertical-rl;
3
+ -epub-writing-mode: vertical-rl;
4
+ -webkit-writing-mode: vertical-rl;
5
+
6
+ text-combine-upright: digits;
7
+ text-align: justify;
8
+ }
9
+
10
+ p {
11
+ margin: 0;
12
+ }
13
+
14
+ hr {
15
+ border: 0;
16
+ margin: 0 1em;
17
+ }
18
+
19
+ em {
20
+ font-style: normal;
21
+ text-emphasis-style: filled sesame;
22
+ -webkit-text-emphasis-style: filled sesame;
23
+ }
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE html>
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="<%= config[:language] %>" lang="<%= config[:language] %>">
4
+ <head>
5
+ <meta charset="utf-8" />
6
+ <title><%= resource.title || resource.basename %></title>
7
+ <% if config.style %>
8
+ <link rel="stylesheet" type="text/css" href="<%= config.style.relative_path_from(resource.dirname) %>" />
9
+ <% end %>
10
+ </head>
11
+ <body epub:type="bodymatter">
12
+ <%= body %>
13
+ </body>
14
+ </html>
@@ -0,0 +1 @@
1
+ <%= body %>
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/makimono/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'makimono'
7
+ spec.version = Makimono::VERSION
8
+ spec.authors = ['Fuji Nakahara']
9
+ spec.email = ['fujinakahara2032@gmail.com']
10
+
11
+ spec.summary = 'Simple ebook generator'
12
+ spec.homepage = 'https://github.com/fuji-nakahara/makimono'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/fuji-nakahara/makimono'
18
+ spec.metadata['changelog_uri'] = 'https://github.com/fuji-nakahara/makimono/blob/master/CHANGELOG.md'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'commonmarker', '>= 0.21'
30
+ spec.add_dependency 'fuji_markdown', '>= 0.3'
31
+ spec.add_dependency 'gepub', '>= 1.0'
32
+ spec.add_dependency 'thor', '>= 1.0'
33
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: makimono
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fuji Nakahara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: commonmarker
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.21'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0.21'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fuji_markdown
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gepub
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description:
70
+ email:
71
+ - fujinakahara2032@gmail.com
72
+ executables:
73
+ - makimono
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".github/workflows/ruby.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".rubocop.yml"
81
+ - CHANGELOG.md
82
+ - CODE_OF_CONDUCT.md
83
+ - Gemfile
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - exe/makimono
90
+ - lib/makimono.rb
91
+ - lib/makimono/cli.rb
92
+ - lib/makimono/command/build.rb
93
+ - lib/makimono/command/new.rb
94
+ - lib/makimono/configuration.rb
95
+ - lib/makimono/converter.rb
96
+ - lib/makimono/converter/markdown.rb
97
+ - lib/makimono/converter/markdown/commonmarker.rb
98
+ - lib/makimono/converter/markdown/fuji_markdown.rb
99
+ - lib/makimono/error.rb
100
+ - lib/makimono/generator.rb
101
+ - lib/makimono/generator/epub.rb
102
+ - lib/makimono/generator/simple_file.rb
103
+ - lib/makimono/processor.rb
104
+ - lib/makimono/resource.rb
105
+ - lib/makimono/resource_list.rb
106
+ - lib/makimono/style.rb
107
+ - lib/makimono/version.rb
108
+ - lib/project_template/.gitignore
109
+ - lib/project_template/Gemfile
110
+ - lib/project_template/makimono.yml
111
+ - lib/project_template/src/01-Title.md
112
+ - lib/styles/fuji.css
113
+ - lib/styles/fuji_tategaki.css
114
+ - lib/templates/default.xhtml.erb
115
+ - lib/templates/empty.txt.erb
116
+ - makimono.gemspec
117
+ homepage: https://github.com/fuji-nakahara/makimono
118
+ licenses:
119
+ - MIT
120
+ metadata:
121
+ homepage_uri: https://github.com/fuji-nakahara/makimono
122
+ source_code_uri: https://github.com/fuji-nakahara/makimono
123
+ changelog_uri: https://github.com/fuji-nakahara/makimono/blob/master/CHANGELOG.md
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 2.6.0
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.1.2
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Simple ebook generator
143
+ test_files: []