booklab-toc 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 112b117e599d6b18e064fcd17a06e3c6981c56c6d2373471eec7a073946baf03
4
+ data.tar.gz: 36ea1bac90ca58f3ed16297e41054edd576a11d7e064b61dd2a088c5be584408
5
+ SHA512:
6
+ metadata.gz: ae829cd9b40fada469f54a2c3df33423ec04b3c74022fc978486f38a43e1f483fa2065c8a7fe46658909375627df31a536128ebb73db8aec6c00f58c5335e8b0
7
+ data.tar.gz: 48e65eaf8b50db91e27c700ab7b73049f95e1c4b786bf31f9d27b62c4e57e8ef95b24025e53a2bb24b378f11f2d8dd93fd2546d90c48b4f130630a9ab45dd0fc
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Jason Lee
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # BookLab::Toc
2
+
3
+ Book TOC read/write tool.
4
+
5
+ ## Installation
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'booklab-toc'
10
+ ```
11
+
12
+ And then execute:
13
+ ```bash
14
+ $ bundle
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```rb
20
+ @content = BookLab::Toc.parse(read_file("sample.yml"))
21
+ @content.each do |item|
22
+ puts item.id
23
+ puts item.title
24
+ puts item.url
25
+ puts item.depth
26
+ end
27
+
28
+ @content = BookLab::Toc.parse(read_file("sample.json"), format: :json)
29
+ @content.to_html
30
+ @content.to_markdown
31
+ @content.to_json
32
+ @content = BookLab::Toc.parse(read_file("sample.md"), format: :markdown)
33
+ @content.to_html(prefix: "https://booklab.io/booklab/docs/")
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ Contribution directions go here.
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Barboon'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,7 @@
1
+ <%- if format == :html -%>
2
+ <ul class="toc-items">
3
+ <%= render partial: "booklab/toc/#{format}/list_item", collection: items, as: :item -%>
4
+ </ul>
5
+ <% else %>
6
+ <%= render partial: "booklab/toc/#{format}/list_item", collection: items, as: :item -%>
7
+ <% end %>
@@ -0,0 +1,11 @@
1
+ <li class="toc-item toc-item-<%= item.depth %>" data-id="<%= item.id %>">
2
+ <%- if item.url -%>
3
+ <a href="<%= item.url %>" class="item-link">
4
+ <span class="item-title"><%= item.title %></span>
5
+ </a>
6
+ <a href="<%= item.url %>" class="item-slug"><%= item.slug %></a>
7
+ <%- else -%>
8
+ <span class="item-link"><span class="item-title"><%= item.title %></span></span>
9
+ <a class="item-slug"><%= item.slug %></span>
10
+ <%- end -%>
11
+ </li>
@@ -0,0 +1,8 @@
1
+ <%-
2
+ indent = " " * item.depth
3
+ -%>
4
+ <%- if item.url -%>
5
+ <%= indent %>- [<%= item.title %>](<%= item.url %>)
6
+ <%- else -%>
7
+ <%= indent %>- <%= item.title %>
8
+ <%- end -%>
@@ -0,0 +1,53 @@
1
+ module BookLab
2
+ module Toc
3
+ class Content
4
+ extend Forwardable
5
+
6
+ attr_accessor :items
7
+
8
+ def_delegators :@items, :[], :size, :<<, :map, :each, :as_json
9
+
10
+ def _dump
11
+ YAML.dump(items.as_json)
12
+ end
13
+
14
+ def to_yaml
15
+ _dump
16
+ end
17
+
18
+ def to_html(prefix: nil)
19
+ _render(format: :html, prefix: prefix)
20
+ end
21
+
22
+ def to_markdown(prefix: nil)
23
+ _render(format: :markdown, prefix: prefix)
24
+ end
25
+
26
+ def to_json
27
+ items.to_json
28
+ end
29
+
30
+ def _render(format: :html, prefix: nil)
31
+ render_items = []
32
+ items.each do |item|
33
+ new_item = item.dup
34
+
35
+ new_item.slug = new_item.url
36
+ if prefix && new_item.url && !new_item.slug.include?("/")
37
+ new_item.url = "#{prefix}#{new_item.slug}"
38
+ end
39
+ render_items << new_item
40
+ end
41
+
42
+ ApplicationController.renderer.render(partial: "booklab/toc/content", locals: {
43
+ format: format,
44
+ items: render_items,
45
+ })
46
+ end
47
+
48
+ def initialize(items)
49
+ @items = items
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ require "rails/engine"
2
+
3
+ module BookLab
4
+ module Toc
5
+ class Engine < Rails::Engine
6
+ isolate_namespace BookLab::Toc
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ module BookLab
2
+ module Toc
3
+ module Format
4
+ class Markdown
5
+ class << self
6
+ def load(raw)
7
+ items = []
8
+ lines = raw.split("\n")
9
+ lines.each do |line|
10
+ item = { "title" => nil, "url" => nil, "depth" => 0 }
11
+ # replace * prefix to - prefix
12
+ line = line.gsub(/^([\s]+)\*/, "\\1-")
13
+ # calculate indent as depth
14
+ item["depth"] = (line.match(/^([\s]+)/).to_s.length / 2.0).round
15
+
16
+ # check line is include link
17
+ m = line.match(/\[(?<title>.+?)\]\((?<url>.+?)\)/)
18
+
19
+ if m
20
+ item["title"] = m[:title]
21
+ # pick up link
22
+ item["url"] = m[:url].split(" ")[0]
23
+ else
24
+ # use chars after "-" as title
25
+ item["title"] = line.gsub(/^[\s]?\-[\s]?/, "").strip
26
+ end
27
+
28
+ items << item
29
+ end
30
+
31
+ items
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,14 @@
1
+ module BookLab
2
+ module Toc
3
+ class ListItem
4
+ attr_accessor :title, :url, :slug, :depth, :id
5
+
6
+ def initialize(title:, url: nil, depth: 0, id: nil)
7
+ @title = title
8
+ @url = url
9
+ @depth = depth
10
+ @id = id
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module BookLab
2
+ module Toc
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ require "booklab/toc/engine"
2
+ require "booklab/toc/list_item"
3
+ require "booklab/toc/content"
4
+ require "booklab/toc/format/markdown"
5
+
6
+ module BookLab
7
+ module Toc
8
+ extend ActiveSupport::Autoload
9
+
10
+
11
+ def self.parse(raw, format: :yml)
12
+ items = []
13
+ datas = []
14
+
15
+ case format
16
+ when :yml
17
+ datas = YAML.load(raw)
18
+ when :json
19
+ datas = JSON.load(raw)
20
+ when :markdown
21
+ datas = Format::Markdown.load(raw)
22
+ else
23
+ raise "format: #{format} not implement"
24
+ end
25
+
26
+ datas.each do |obj|
27
+ items << ListItem.new(title: obj["title"], url: obj["url"], depth: obj["depth"], id: obj["id"])
28
+ end
29
+ Content.new(items)
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: booklab-toc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Toc format read/write for BookLab
42
+ email:
43
+ - huacnlee@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/views/booklab/toc/_content.html.erb
52
+ - app/views/booklab/toc/html/_list_item.erb
53
+ - app/views/booklab/toc/markdown/_list_item.erb
54
+ - lib/booklab-toc.rb
55
+ - lib/booklab/toc/content.rb
56
+ - lib/booklab/toc/engine.rb
57
+ - lib/booklab/toc/format/markdown.rb
58
+ - lib/booklab/toc/list_item.rb
59
+ - lib/booklab/toc/version.rb
60
+ homepage: https://github.com/huacnlee/barboom
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.7.7
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Toc format read/write for BookLab
84
+ test_files: []