bluedoc-toc 0.3.1

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: 5f70a76b50ad3ef91b561ebd2d39d53e3388aa1c599ab97525b5cfbd1a165ada
4
+ data.tar.gz: c9d94dc3cda4bfce1f3e184f10a347a7cb545598ca37764bccdb806f573feeac
5
+ SHA512:
6
+ metadata.gz: debd730297d4f5bad7626501d58dd90bb581f62e9ec602bbf1fe73b59249f11a47922c0f90992ec192a23e891edaf02ad3a5b69b624387c8f8f60867d970de8c
7
+ data.tar.gz: c5ff460102f494469457864d533672346f9376ec47a42e2d29b0731a5c59e58c7f5b548e4aca8fa15f0ce1ca39d9187c9c8b832002d4ff9d7207783a6b3376bd
@@ -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.
@@ -0,0 +1,42 @@
1
+ # BlueDoc::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 'bluedoc-toc'
10
+ ```
11
+
12
+ And then execute:
13
+ ```bash
14
+ $ bundle
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```rb
20
+ @content = BlueDoc::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 = BlueDoc::Toc.parse(read_file("sample.json"), format: :json)
29
+ @content.to_html
30
+ @content.to_markdown
31
+ @content.to_json
32
+ @content = BlueDoc::Toc.parse(read_file("sample.md"), format: :markdown)
33
+ @content.to_html(prefix: "https://bluedoc.io/bluedoc/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).
@@ -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: "bluedoc/toc/#{format}/list_item", collection: items, as: :item -%>
4
+ </ul>
5
+ <% else %>
6
+ <%= render partial: "bluedoc/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
+ <span 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,43 @@
1
+ require "bluedoc/toc/engine"
2
+ require "bluedoc/toc/list_item"
3
+ require "bluedoc/toc/content"
4
+ require "bluedoc/toc/format/markdown"
5
+
6
+ module BlueDoc
7
+ module Toc
8
+ class FormatError < Exception; end
9
+
10
+ extend ActiveSupport::Autoload
11
+
12
+ class << self
13
+ delegate :logger, to: Rails
14
+
15
+ def parse(raw, format: :yml, strict: false)
16
+ items = []
17
+ datas = []
18
+
19
+ case format
20
+ when :yml
21
+ datas = YAML.load(raw)
22
+ when :json
23
+ datas = JSON.load(raw)
24
+ when :markdown
25
+ datas = Format::Markdown.load(raw)
26
+ else
27
+ raise "format: #{format} not implement"
28
+ end
29
+
30
+ datas.each do |obj|
31
+ item_hash = obj.to_hash
32
+ item_hash.deep_symbolize_keys!
33
+ items << ListItem.new(id: item_hash[:id], title: item_hash[:title], url: item_hash[:url], depth: item_hash[:depth])
34
+ end
35
+ Content.new(items)
36
+ rescue => e
37
+ raise FormatError.new(e.message) if strict
38
+ logger.warn "BlueDoc::Toc.parse error:\n#{e.inspect}"
39
+ Content.new([])
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,57 @@
1
+ module BlueDoc
2
+ module Toc
3
+ class Content
4
+ extend Forwardable
5
+
6
+ attr_accessor :items
7
+
8
+ def_delegators :@items, :[], :size, :<<, :map, :each, :each_with_index, :as_json, :find, :sort, :sort_by, :take, :first
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, suffix: nil)
19
+ _render(format: :html, prefix: prefix, suffix: suffix)
20
+ end
21
+
22
+ def to_markdown(prefix: nil, suffix: nil)
23
+ _render(format: :markdown, prefix: prefix, suffix: suffix)
24
+ end
25
+
26
+ def to_json
27
+ items.to_json
28
+ end
29
+
30
+ def _render(format: :html, prefix: nil, suffix: nil)
31
+ render_items = []
32
+ items.each do |item|
33
+ new_item = item.dup
34
+
35
+ new_item.slug = new_item.url
36
+ if new_item.url && !new_item.slug.include?("/")
37
+ new_item.url = "#{prefix.to_s}#{new_item.slug}#{suffix.to_s}"
38
+ end
39
+ render_items << new_item
40
+ end
41
+
42
+ ApplicationController.renderer.render(partial: "bluedoc/toc/content", locals: {
43
+ format: format,
44
+ items: render_items,
45
+ })
46
+ end
47
+
48
+ def find_by_url(url)
49
+ items.find { |item| item.url&.strip == url&.strip }
50
+ end
51
+
52
+ def initialize(items)
53
+ @items = items
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,9 @@
1
+ require "rails/engine"
2
+
3
+ module BlueDoc
4
+ module Toc
5
+ class Engine < Rails::Engine
6
+ isolate_namespace BlueDoc::Toc
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,46 @@
1
+ module BlueDoc
2
+ module Toc
3
+ module Format
4
+ class Markdown
5
+ class << self
6
+ def load(raw)
7
+ items = []
8
+
9
+ # check indent style, 2 spaces/4 spaces/1 tab
10
+ space_size = 4
11
+ if raw.match(/^[\t]{1}[\s]?[-\*]/m)
12
+ space_size = 1
13
+ elsif raw.match(/^[\s]{2}[\s]?[-\*]/m)
14
+ space_size = 2
15
+ end
16
+
17
+ lines = raw.split("\n")
18
+ lines.each do |line|
19
+ item = { "title" => nil, "url" => nil, "depth" => 0 }
20
+ # replace * prefix to - prefix
21
+ line = line.gsub(/^([\s]+)\*/, "\\1-")
22
+ # calculate indent as depth
23
+ item["depth"] = (line.match(/^([\s]+)/).to_s.length / space_size).round
24
+
25
+ # check line is include link
26
+ m = line.match(/\[(?<title>.+?)\]\((?<url>.+?)\)/)
27
+
28
+ if m
29
+ item["title"] = m[:title]
30
+ # pick up link
31
+ item["url"] = m[:url].split(" ")[0]
32
+ else
33
+ # use chars after "-" as title
34
+ item["title"] = line.gsub(/^[\s]?\-[\s]?/, "").strip
35
+ end
36
+
37
+ items << item
38
+ end
39
+
40
+ items
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ module BlueDoc
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 BlueDoc
2
+ module Toc
3
+ VERSION = '0.3.1'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bluedoc-toc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - Jason Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-15 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: '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: 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 BlueDoc
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/bluedoc-toc.rb
55
+ - lib/bluedoc/toc/content.rb
56
+ - lib/bluedoc/toc/engine.rb
57
+ - lib/bluedoc/toc/format/markdown.rb
58
+ - lib/bluedoc/toc/list_item.rb
59
+ - lib/bluedoc/toc/version.rb
60
+ homepage: https://github.com/thebluedoc/bluedoc-toc
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
+ rubygems_version: 3.0.1
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Toc format read/write for BlueDoc
83
+ test_files: []