headown 0.1.0 → 0.2.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: f74423e86a239a4baf00b000cf0ff3fd13ab4af7082750388faa8c462a7ccf20
4
+ data.tar.gz: ed615e669ad789158a317dba43c7234793f23e2e76ec693ce626466aaab63c31
5
5
  SHA512:
6
- metadata.gz: 78d66b6d263ecf33aa389cc72a1419d8390aafafaa7c4ce2e052e2acf0293d2d943a6bd7851ed5a818a7b2858b8677ed82ac0cd59db26f5c0b0bca8d4609bc24
7
- data.tar.gz: 951d0697a49ded9d3d9d33bab2c86d525c4135fdb5425db76b410a100751fb20f92a250cfa0782636f1b1931c5a701a7bd579ceeb2886c636cc9691cd0bdb20c
6
+ metadata.gz: 788b6416a14427891723fad67e53645514e6eeb647443f81f0f3ac18bcff177d0dc3d83de7cb469c49022626db239632d27cb615b6d458a82d3c16d1f7ce9f08
7
+ data.tar.gz: f6c74ca98eeb4efc4236784a56368d15f6f1ab06908c88f74f6fed9e0e7cf2249e0f1502c7b803fbd47c1b5c4f10c57a9ddc0893e1ea4933a3e28b7be162a67e
@@ -0,0 +1,38 @@
1
+ {{ range .Versions }}
2
+ <a name="{{ .Tag.Name }}"></a>
3
+ ## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }} ({{ datetime "2006-01-02" .Tag.Date }})
4
+
5
+ {{ range .CommitGroups -}}
6
+ ### {{ .Title }}
7
+
8
+ {{ range .Commits -}}
9
+ * {{ .Subject }}
10
+ {{ end }}
11
+ {{ end -}}
12
+
13
+ {{- if .RevertCommits -}}
14
+ ### Reverts
15
+
16
+ {{ range .RevertCommits -}}
17
+ * {{ .Revert.Header }}
18
+ {{ end }}
19
+ {{ end -}}
20
+
21
+ {{- if .MergeCommits -}}
22
+ ### Pull Requests
23
+
24
+ {{ range .MergeCommits -}}
25
+ * {{ .Header }}
26
+ {{ end }}
27
+ {{ end -}}
28
+
29
+ {{- if .NoteGroups -}}
30
+ {{ range .NoteGroups -}}
31
+ ### {{ .Title }}
32
+
33
+ {{ range .Notes }}
34
+ {{ .Body }}
35
+ {{ end }}
36
+ {{ end -}}
37
+ {{ end -}}
38
+ {{ end -}}
@@ -0,0 +1,27 @@
1
+ style: github
2
+ template: CHANGELOG.tpl.md
3
+ info:
4
+ title: CHANGELOG
5
+ repository_url: https://github.com/noriyotcp/headown
6
+ options:
7
+ commits:
8
+ # filters:
9
+ # Type:
10
+ # - feat
11
+ # - fix
12
+ # - perf
13
+ # - refactor
14
+ commit_groups:
15
+ # title_maps:
16
+ # feat: Features
17
+ # fix: Bug Fixes
18
+ # perf: Performance Improvements
19
+ # refactor: Code Refactoring
20
+ header:
21
+ pattern: "^((\\w+)\\s.*)$"
22
+ pattern_maps:
23
+ - Subject
24
+ - Type
25
+ notes:
26
+ keywords:
27
+ - BREAKING CHANGE
@@ -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: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ main ]
13
+ pull_request:
14
+ branches: [ main ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ - name: Set up Ruby
24
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
+ # uses: ruby/setup-ruby@v1
27
+ uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
28
+ with:
29
+ ruby-version: 2.7
30
+ - name: Install dependencies
31
+ run: bundle install
32
+ - name: Run tests
33
+ run: bundle exec rake
@@ -0,0 +1,61 @@
1
+ <a name="v0.2.0"></a>
2
+ ## [v0.2.0](https://github.com/noriyotcp/headown/compare/v0.1.0...v0.2.0) (2020-10-03)
3
+
4
+ ### Add
5
+
6
+ * Add Headown::Extractor which has logic to extract headers
7
+ * Add CHANGELOG.md
8
+ * Add .chglog which contains git-chglog's config and template files
9
+
10
+ ### Create
11
+
12
+ * Create ruby.yml
13
+
14
+ ### Improve
15
+
16
+ * Improve test against html file path
17
+ * Raise Headown::NotMarkdownError in constructor in Headown::Extractor
18
+ * Raise Headown::NotMarkdownError when file path is not Markdown file
19
+
20
+ ### Update
21
+
22
+ * Move require 'headown/cli' to exe/headown
23
+ * Update README.md
24
+ * Update CHANGELOG.md
25
+
26
+ ### Pull Requests
27
+
28
+ * Merge pull request [#4](https://github.com/noriyotcp/headown/issues/4) from noriyotcp/noriyotcp-patch-1
29
+ * Merge pull request [#3](https://github.com/noriyotcp/headown/issues/3) from noriyotcp/extractor-class
30
+ * Merge pull request [#2](https://github.com/noriyotcp/headown/issues/2) from noriyotcp/extract-require-headown-cli
31
+ * Merge pull request [#1](https://github.com/noriyotcp/headown/issues/1) from noriyotcp/detect-file-extension
32
+
33
+ <a name="v0.1.0"></a>
34
+ ## v0.1.0 (2020-09-27)
35
+
36
+ ### Add
37
+
38
+ * Add sample2.md to test against interruption paragraphs for ATX headings
39
+ * Add test against passing url
40
+ * Add puts_headers to print headers out to stdout
41
+
42
+ ### Exec
43
+
44
+ * Exec bundle exec rbprettier --write '**/*.rb'
45
+
46
+ ### Improve
47
+
48
+ * Extract from sample markdown file
49
+ * Improve sample.md
50
+
51
+ ### Install
52
+
53
+ * Install prettier gem
54
+ * Install thor
55
+ * Install gems
56
+
57
+ ### Update
58
+
59
+ * Update README.md
60
+ * Update gemspec
61
+ * Update headown.gemspec
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- headown (0.1.0)
4
+ headown (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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.
@@ -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)
@@ -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
@@ -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.2.0'
3
3
  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.2.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: 2020-10-03 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,10 +18,14 @@ executables:
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - ".chglog/CHANGELOG.tpl.md"
22
+ - ".chglog/config.yml"
23
+ - ".github/workflows/ruby.yml"
21
24
  - ".gitignore"
22
25
  - ".prettierrc.json"
23
26
  - ".rspec"
24
27
  - ".travis.yml"
28
+ - CHANGELOG.md
25
29
  - CODE_OF_CONDUCT.md
26
30
  - Gemfile
27
31
  - Gemfile.lock