headown 0.1.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 605fa3f8a4bbc3fc2a4f4bf2111ce8300d2d0bcc42d3e6b8fd14c445c97e3746
4
- data.tar.gz: 03c2bee496d9b0faf82df64d23214b0487120915c128be88a2297759b710afdb
3
+ metadata.gz: 541a08c3b53c77ecdf85ffd2304f159c3d77c8fe035015476132faa3d1eea08a
4
+ data.tar.gz: bd9a343fec1e22785efc3c0dddd6b3799b0bd6cc6c3ee065ed93cb084dfdcbab
5
5
  SHA512:
6
- metadata.gz: 78d66b6d263ecf33aa389cc72a1419d8390aafafaa7c4ce2e052e2acf0293d2d943a6bd7851ed5a818a7b2858b8677ed82ac0cd59db26f5c0b0bca8d4609bc24
7
- data.tar.gz: 951d0697a49ded9d3d9d33bab2c86d525c4135fdb5425db76b410a100751fb20f92a250cfa0782636f1b1931c5a701a7bd579ceeb2886c636cc9691cd0bdb20c
6
+ metadata.gz: 47ef832759faba71f2c39a7909f98d428b544da18b1ddf30ddc729e9d4d7a722c4a144bceb65720e4523111b22b48229989339e3084dab9a8ef7603ecb8076ef
7
+ data.tar.gz: 0d21103d9d5e864bd957b45c7c974523f33a78b93c7adad8d5d5b058bbcfa213c6a261494bdf644c2cc41e725edefe5d901a06830d6ccc35ad4adb680f095031
@@ -0,0 +1,33 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Setup Ruby
9
+ on:
10
+ push:
11
+ branches: [ main ]
12
+ pull_request:
13
+ branches: [ main ]
14
+
15
+ jobs:
16
+ test:
17
+ strategy:
18
+ fail-fast: false
19
+ matrix:
20
+ os: [ubuntu-latest, macos-latest, windows-latest]
21
+ # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
22
+ ruby: ['2.7', '3.0', '3.1']
23
+ runs-on: ${{ matrix.os }}
24
+ steps:
25
+ - uses: actions/checkout@v3
26
+ - uses: ruby/setup-ruby@v1
27
+ with:
28
+ ruby-version: ${{ matrix.ruby }}
29
+ bundler-cache: true
30
+ - name: Run tests
31
+ run: bundle exec rake
32
+ - name: Type checking
33
+ run: bundle exec steep check
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ https://github.com/noriyotcp/headown/releases
data/Gemfile CHANGED
@@ -3,7 +3,7 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in headown.gemspec
4
4
  gemspec
5
5
 
6
- gem 'rake', '~> 12.0'
6
+ gem 'rake'
7
7
  gem 'rspec', '~> 3.0'
8
8
  gem 'commonmarker'
9
9
  gem 'thor'
@@ -11,3 +11,7 @@ gem 'thor'
11
11
  group :development do
12
12
  gem 'prettier'
13
13
  end
14
+
15
+ group :development, :test do
16
+ gem 'steep'
17
+ end
data/Gemfile.lock CHANGED
@@ -1,35 +1,88 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- headown (0.1.0)
4
+ headown (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- commonmarker (0.21.0)
10
- ruby-enum (~> 0.5)
11
- concurrent-ruby (1.1.7)
12
- diff-lcs (1.4.4)
13
- i18n (1.8.5)
9
+ activesupport (7.0.4)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ ast (2.4.2)
15
+ commonmarker (0.23.6)
16
+ concurrent-ruby (1.1.10)
17
+ diff-lcs (1.5.0)
18
+ ffi (1.15.5)
19
+ haml (6.0.7)
20
+ temple (>= 0.8.2)
21
+ thor
22
+ tilt
23
+ i18n (1.12.0)
14
24
  concurrent-ruby (~> 1.0)
15
- prettier (0.20.1)
16
- rake (12.3.3)
17
- rspec (3.9.0)
18
- rspec-core (~> 3.9.0)
19
- rspec-expectations (~> 3.9.0)
20
- rspec-mocks (~> 3.9.0)
21
- rspec-core (3.9.2)
22
- rspec-support (~> 3.9.3)
23
- rspec-expectations (3.9.2)
25
+ language_server-protocol (3.17.0.1)
26
+ listen (3.7.1)
27
+ rb-fsevent (~> 0.10, >= 0.10.3)
28
+ rb-inotify (~> 0.9, >= 0.9.10)
29
+ minitest (5.16.3)
30
+ parallel (1.22.1)
31
+ parser (3.1.2.1)
32
+ ast (~> 2.4.1)
33
+ prettier (3.2.2)
34
+ syntax_tree (>= 2.7.1)
35
+ syntax_tree-haml (>= 1.1.0)
36
+ syntax_tree-rbs (>= 0.2.0)
37
+ prettier_print (0.1.0)
38
+ rainbow (3.1.1)
39
+ rake (13.0.6)
40
+ rb-fsevent (0.11.2)
41
+ rb-inotify (0.10.1)
42
+ ffi (~> 1.0)
43
+ rbs (2.7.0)
44
+ rspec (3.11.0)
45
+ rspec-core (~> 3.11.0)
46
+ rspec-expectations (~> 3.11.0)
47
+ rspec-mocks (~> 3.11.0)
48
+ rspec-core (3.11.0)
49
+ rspec-support (~> 3.11.0)
50
+ rspec-expectations (3.11.1)
24
51
  diff-lcs (>= 1.2.0, < 2.0)
25
- rspec-support (~> 3.9.0)
26
- rspec-mocks (3.9.1)
52
+ rspec-support (~> 3.11.0)
53
+ rspec-mocks (3.11.1)
27
54
  diff-lcs (>= 1.2.0, < 2.0)
28
- rspec-support (~> 3.9.0)
29
- rspec-support (3.9.3)
30
- ruby-enum (0.8.0)
31
- i18n
32
- thor (1.0.1)
55
+ rspec-support (~> 3.11.0)
56
+ rspec-support (3.11.1)
57
+ securerandom (0.2.0)
58
+ steep (1.2.0)
59
+ activesupport (>= 5.1)
60
+ language_server-protocol (>= 3.15, < 4.0)
61
+ listen (~> 3.0)
62
+ parallel (>= 1.0.0)
63
+ parser (>= 3.1)
64
+ rainbow (>= 2.2.2, < 4.0)
65
+ rbs (>= 2.7.0)
66
+ securerandom (>= 0.1)
67
+ terminal-table (>= 2, < 4)
68
+ syntax_tree (3.6.3)
69
+ prettier_print
70
+ syntax_tree-haml (1.3.2)
71
+ haml (>= 5.2)
72
+ prettier_print
73
+ syntax_tree (>= 2.0.1)
74
+ syntax_tree-rbs (0.5.1)
75
+ prettier_print
76
+ rbs
77
+ syntax_tree (>= 2.0.1)
78
+ temple (0.8.2)
79
+ terminal-table (3.0.2)
80
+ unicode-display_width (>= 1.1.1, < 3)
81
+ thor (1.2.1)
82
+ tilt (2.0.11)
83
+ tzinfo (2.0.5)
84
+ concurrent-ruby (~> 1.0)
85
+ unicode-display_width (2.3.0)
33
86
 
34
87
  PLATFORMS
35
88
  ruby
@@ -38,9 +91,10 @@ DEPENDENCIES
38
91
  commonmarker
39
92
  headown!
40
93
  prettier
41
- rake (~> 12.0)
94
+ rake
42
95
  rspec (~> 3.0)
96
+ steep
43
97
  thor
44
98
 
45
99
  BUNDLED WITH
46
- 2.1.2
100
+ 2.3.10
data/README.md CHANGED
@@ -20,7 +20,7 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- From this following file
23
+ From this following file:
24
24
 
25
25
  ```markdown
26
26
  # h1
@@ -45,11 +45,11 @@ This is h5.
45
45
 
46
46
  ###### h6
47
47
 
48
- This is h5.
48
+ This is h6.
49
49
 
50
50
  ```
51
51
 
52
- Extracts headers only
52
+ Extracts headers only:
53
53
 
54
54
  ```shell script
55
55
  $ headown extract ./spec/headown/sample.md
@@ -61,7 +61,7 @@ $ headown extract ./spec/headown/sample.md
61
61
  ###### h6
62
62
  ```
63
63
 
64
- From online
64
+ From online:
65
65
 
66
66
  ```shell script
67
67
  $ headown extract https://raw.githubusercontent.com/noriyotcp/headown/main/spec/headown/sample.md
@@ -73,7 +73,7 @@ $ headown extract https://raw.githubusercontent.com/noriyotcp/headown/main/spec/
73
73
  ###### h6
74
74
  ```
75
75
 
76
- ATX headings can interrupt paragraphs
76
+ ATX headings can interrupt paragraphs:
77
77
 
78
78
  ```markdown
79
79
  ****
@@ -85,12 +85,23 @@ ATX headings can interrupt paragraphs
85
85
  <hr />
86
86
  ```
87
87
 
88
- So, it extracts headers
88
+ So, it extracts headers:
89
89
 
90
90
  ```shell script
91
91
  $ headown extract ./spec/headown/sample2.md
92
92
  # baz
93
93
  ```
94
+
95
+ You can also use it in a Ruby file:
96
+
97
+ ```ruby
98
+ require 'headown'
99
+ #=> true
100
+ extractor = Headown::Extractor.new('spec/headown/sample.md')
101
+ extractor.headers
102
+ #=> ["# h1", "## h2", "### h3", "#### h4", "##### h5", "###### h6"]
103
+ ```
104
+
94
105
  ## Development
95
106
 
96
107
  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.
data/Steepfile ADDED
@@ -0,0 +1,23 @@
1
+ # D = Steep::Diagnostic
2
+ #
3
+ target :lib do
4
+ signature "sig"
5
+ check "lib" # Directory name
6
+ # # check "Gemfile" # File name
7
+ # # library "pathname", "set" # Standard libraries
8
+ # # library "strong_json" # Gems
9
+ #
10
+ # # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
11
+ # # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
12
+ # # configure_code_diagnostics do |hash| # You can setup everything yourself
13
+ # # hash[D::Ruby::NoMethod] = :information
14
+ # # end
15
+ end
16
+
17
+ # target :test do
18
+ # signature "sig", "sig-private"
19
+ #
20
+ # check "test"
21
+ #
22
+ # # library "pathname", "set" # Standard libraries
23
+ # end
data/exe/headown CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "headown"
3
+ require 'headown'
4
+ require 'headown/cli'
4
5
 
5
6
  Headown::CLI.start(ARGV)
data/headown.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.description = 'This is the CLI tool extracting headers from Markdown files.'
11
11
  spec.homepage = 'https://github.com/noriyotcp/headown'
12
12
  spec.license = 'MIT'
13
- spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
14
14
 
15
15
  spec.metadata['allowed_push_host'] = 'https://rubygems.org'
16
16
 
data/lib/headown/cli.rb CHANGED
@@ -1,35 +1,17 @@
1
1
  require 'thor'
2
- require 'commonmarker'
3
- require 'open-uri'
4
2
 
5
3
  module Headown
6
4
  class CLI < Thor
7
5
  desc 'extract <path>', 'extract headers from file path'
8
6
 
9
7
  def extract(file_path)
10
- file_data = URI.open(file_path, &:read)
11
- puts_headers(file_data)
12
- end
13
-
14
- private
15
-
16
- def header_nodes(data)
17
- doc = CommonMarker.render_doc(data)
18
- [].tap { |nodes| doc.walk { |node| nodes << node if node.type == :header } }
19
- end
20
-
21
- def build_header(node)
22
- text = ''
23
- node.each { |subnode| text = subnode.string_content if subnode.type == :text }
24
- '#' * node.header_level + " #{text}"
25
- end
26
-
27
- def extract_headers(file_data)
28
- header_nodes(file_data).map { |node| build_header(node) }
29
- end
30
-
31
- def puts_headers(file_data)
32
- puts extract_headers(file_data)
8
+ extractor = Headown::Extractor.new(file_path)
9
+ puts extractor.headers
10
+ rescue Headown::NotMarkdownError => e
11
+ puts <<~MSG
12
+ #{e.class}:
13
+ #{e.message}
14
+ MSG
33
15
  end
34
16
  end
35
17
  end
@@ -1,3 +1,3 @@
1
1
  module Headown
2
- VERSION = '0.1.0'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/headown.rb CHANGED
@@ -1,6 +1,40 @@
1
1
  require 'headown/version'
2
- require 'headown/cli'
2
+ require 'open-uri'
3
+ require 'commonmarker'
3
4
 
4
5
  module Headown
5
- class Error < StandardError; end # Your code goes here...
6
+ class NotMarkdownError < StandardError
7
+ def initialize(file_path:)
8
+ msg = "#{file_path} is not a Markdown file."
9
+ super(msg)
10
+ end
11
+ end
12
+
13
+ class Extractor
14
+ attr_reader :headers
15
+
16
+ def initialize(file_path)
17
+ raise Headown::NotMarkdownError.new(file_path: file_path) if File.extname(file_path) != '.md'
18
+
19
+ file_data = URI.open(file_path, &:read)
20
+ extract_headers(file_data)
21
+ end
22
+
23
+ private
24
+
25
+ def extract_headers(file_data)
26
+ @headers = header_nodes(file_data).map { |node| build_header(node) }
27
+ end
28
+
29
+ def header_nodes(data)
30
+ doc = CommonMarker.render_doc(data)
31
+ [].tap { |nodes| doc.walk { |node| nodes << node if node.type == :header } }
32
+ end
33
+
34
+ def build_header(node)
35
+ text = ''
36
+ node.each { |subnode| text = subnode.string_content if subnode.type == :text }
37
+ '#' * node.header_level + " #{text}"
38
+ end
39
+ end
6
40
  end
data/sig/headown.rbs ADDED
@@ -0,0 +1,47 @@
1
+ # TypeProf 0.21.2
2
+
3
+ # polyfill for thor
4
+ class Thor
5
+ def self.desc: (String, String, ?Hash[Symbol, String]) -> void
6
+ end
7
+
8
+ # polyfill for URI
9
+ class URI
10
+ def self.open: (String) { (IO) -> (::String | nil) } -> IO
11
+ end
12
+
13
+ # polyfill for commonmarker
14
+ module CommonMarker
15
+ def self.render_doc: (::IO | nil) -> CommonMarker::Node
16
+
17
+ class Node
18
+ def each: [U] { (Node) -> U } -> ::Array[U]
19
+ def walk: [U] { (Node) -> U } -> ::Array[U]
20
+ attr_reader type: Symbol
21
+ attr_reader string_content: String
22
+ attr_reader header_level: Integer
23
+ end
24
+ end
25
+
26
+ # Classes
27
+ module Headown
28
+ VERSION: String
29
+
30
+ class NotMarkdownError < StandardError
31
+ def initialize: (file_path: String) -> void
32
+ end
33
+
34
+ class Extractor
35
+ attr_reader headers: Array[String]
36
+ def initialize: (String file_path) -> Array[String]
37
+
38
+ private
39
+ def extract_headers: (IO file_data) -> Array[String]
40
+ def header_nodes: (IO data) -> Array[CommonMarker::Node]
41
+ def build_header: (CommonMarker::Node node) -> String
42
+ end
43
+
44
+ class CLI < Thor
45
+ def extract: (String file_path) -> void
46
+ end
47
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: headown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - noriyotcp
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-27 00:00:00.000000000 Z
11
+ date: 2022-10-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This is the CLI tool extracting headers from Markdown files.
14
14
  email:
@@ -18,16 +18,18 @@ executables:
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - ".github/workflows/ruby.yml"
21
22
  - ".gitignore"
22
23
  - ".prettierrc.json"
23
24
  - ".rspec"
24
- - ".travis.yml"
25
+ - CHANGELOG.md
25
26
  - CODE_OF_CONDUCT.md
26
27
  - Gemfile
27
28
  - Gemfile.lock
28
29
  - LICENSE.txt
29
30
  - README.md
30
31
  - Rakefile
32
+ - Steepfile
31
33
  - bin/console
32
34
  - bin/setup
33
35
  - exe/headown
@@ -35,6 +37,7 @@ files:
35
37
  - lib/headown.rb
36
38
  - lib/headown/cli.rb
37
39
  - lib/headown/version.rb
40
+ - sig/headown.rbs
38
41
  homepage: https://github.com/noriyotcp/headown
39
42
  licenses:
40
43
  - MIT
@@ -51,14 +54,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
54
  requirements:
52
55
  - - ">="
53
56
  - !ruby/object:Gem::Version
54
- version: 2.3.0
57
+ version: 2.7.0
55
58
  required_rubygems_version: !ruby/object:Gem::Requirement
56
59
  requirements:
57
60
  - - ">="
58
61
  - !ruby/object:Gem::Version
59
62
  version: '0'
60
63
  requirements: []
61
- rubygems_version: 3.1.2
64
+ rubygems_version: 3.3.7
62
65
  signing_key:
63
66
  specification_version: 4
64
67
  summary: Markdown Headers Extractor
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.0
6
- before_install: gem install bundler -v 2.1.2