clippings 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 92b07bdac3414a78b7a73d38b5225c417529ce47e825b854d8c759b6ecfca1ac
4
+ data.tar.gz: 179599de9d35808b66527fa289f2ec6d66ea1170ef177cc862718c3be84d23c9
5
+ SHA512:
6
+ metadata.gz: a6b63c6eb9c171edee0b279983dff1c5e4bda9608226a186a83058837e436393c67cb662eaf1ab7583f6f5617c12f5a5414f471372765b7220bbc5a8226c4315
7
+ data.tar.gz: 3d92b9383468cb90fab66c6f5f1eecdf9971bf0d0fa8d5dbd4f108d28139d3a2d1e12008dab6e81d670a2a75458a2965ca9e2380299a077960609039f3979295
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in clippings.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 badtudou
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,119 @@
1
+ # Clippings
2
+
3
+ **Clippings** can parse notes and highlights in your Kindle clippings.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'clippings'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install clippings
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'clippings'
25
+ clippings = Clippings::Parse.new("My Clippings.txt")
26
+ notes = clippings.parseForNote
27
+ notes.each do |title, note|
28
+ print title, note[:author], note[:fragment]
29
+ end
30
+ ```
31
+ ## Example
32
+ **test.txt**
33
+ ```
34
+ ==========
35
+ HTTP权威指南 (图灵程序设计丛书) ([美]David Gourley Brian Totty Marjorie Sayer Sailu Reddy Aushu Aggarwal)
36
+ - 您在位置 #425-426的标注 | 添加于 2017年6月16日星期五 下午8:21:59
37
+
38
+ 下报文是如何通过传输控制协议(Transmission Control Protocol,TCP)连接从一个地方搬移到另一个地方去的。
39
+ ==========
40
+ HTTP权威指南 (图灵程序设计丛书) ([美]David Gourley Brian Totty Marjorie Sayer Sailu Reddy Aushu Aggarwal)
41
+ - 您在位置 #802 的笔记 | 添加于 2017年6月16日星期五 下午9:02:31
42
+
43
+ 解析相对url 的算法
44
+ ==========
45
+ The Well-Grounded Rubyist, Second Edition (David A. Black)
46
+ - 您在位置 #7107-7107的标注 | 添加于 2017年9月5日星期二 上午10:13:28
47
+
48
+ Ranges have begin and end methods,
49
+ ==========
50
+ The Well-Grounded Rubyist, Second Edition (David A. Black)
51
+ - 您在位置 #7193-7193的标注 | 添加于 2017年9月5日星期二 上午10:28:42
52
+
53
+ To remove an object, use delete:
54
+ ==========
55
+ ```
56
+
57
+ **Code**
58
+ ```ruby
59
+ require 'clippings'
60
+ clippings = Clippings::Parse.new("test.txt")
61
+ notes = clippings.parseForNote
62
+ p notes
63
+ ```
64
+
65
+ **Output**
66
+ ```
67
+ {
68
+ "HTTP权威指南 (图灵程序设计丛书)"=>
69
+ {
70
+ :title=>"HTTP权威指南 (图灵程序设计丛书)",
71
+ :author=>"[美]David Gourley Brian Totty Marjorie Sayer Sailu Reddy Aushu Aggarwal",
72
+ :fragment=>
73
+ [
74
+ {
75
+ :content=>"HTTP 是个应用层协议\r\n",
76
+ :type=>"标注",
77
+ :date=>" 2017年6月16日星期五 下午8:22:18"
78
+ },
79
+ {
80
+ :content=>"解析相对url 的算法\r\n",
81
+ :type=>"笔记",
82
+ :date=>" 2017年6月16日星期五 下午9:02:31"
83
+ },
84
+ {
85
+ :content=>"RFC 2396 中\r\n",
86
+ :type=>"标注",
87
+ :date=>" 2017年6月16日星期五 下午9:02:31"
88
+ }
89
+ ]
90
+ },
91
+ "The Well-Grounded Rubyist, Second Edition"=>
92
+ {
93
+ :title=>"The Well-Grounded Rubyist, Second Edition",
94
+ :author=>"David A. Black",
95
+ :fragment=>
96
+ [
97
+ {
98
+ :content=>"Ranges have begin and end methods,\r\n",
99
+ :type=>"标注",
100
+ :date=>" 2017年9月5日星期二 上午10:13:28"
101
+ },
102
+ {
103
+ :content=>"To remove an object, use delete:\r\n",
104
+ :type=>"标注",
105
+ :date=>" 2017年9月5日星期二 上午10:28:42"
106
+ }
107
+ ]
108
+ }
109
+ }
110
+ ```
111
+
112
+
113
+ ## Contributing
114
+
115
+ Bug reports and pull requests are welcome on GitHub at https://github.com/badtudou/clippings-gem.
116
+
117
+ ## License
118
+
119
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "clippings"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "clippings/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "clippings"
8
+ spec.version = Clippings::VERSION
9
+ spec.authors = ["badtudou"]
10
+ spec.email = ["badtudou@gmail.com"]
11
+
12
+ spec.summary = %q{Parse notes and highlights in your Kindle clippings}
13
+ spec.description = %q{Parse notes and highlights in your Kindle clippings, the name of the Kindle clipper file was 'My Clippings.txt'}
14
+ spec.homepage = "https://github.com/BadTudou/clippings"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
@@ -0,0 +1,95 @@
1
+ require "clippings/version"
2
+
3
+ module Clippings
4
+ NOTE_SECTION_SEPARATOR = '=========='
5
+ NOTE_DEFAULT_AUTHOR = 'Unknown author'
6
+ NOTE_DEFAULT_DATETIME = '1970-01-01 00:00:00'
7
+ NOTE_LABEL = 'Note|笔记'
8
+ HIGHLIGHT_LABEL = 'Highlight|标注'
9
+ ADD_LABEL = 'Added on|添加于'
10
+
11
+ attr_accessor :filepath, :notes
12
+
13
+ # 解析
14
+ class Parse
15
+
16
+ def initialize(filepath)
17
+ @filepath = filepath
18
+ end
19
+
20
+ # 解析为块
21
+ def parseForSection
22
+ sections = []
23
+ if File.exist?(@filepath)
24
+ section = ''
25
+ File.open(@filepath, 'r') do |file|
26
+ file.each_line do |line|
27
+ if line.start_with?(NOTE_SECTION_SEPARATOR)
28
+ sections.push(section)
29
+ section = ''
30
+ else
31
+ section.concat(line)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ sections.delete_at(0)
37
+ return sections
38
+ end
39
+
40
+ # 解析为片段
41
+ def parseForFragment(sections = nil)
42
+ sections ||= parseForSection()
43
+ fragments = []
44
+ sections.each do |section|
45
+ fragment = {}
46
+ fragment[:title_author], fragment[:type_date], fragment[:content] = section.split("\r", 3)
47
+
48
+ # 提取片段的标题与作者
49
+ /(.*)\((.*)\).*/ =~ fragment[:title_author]
50
+ fragment[:title] = $1 || fragment[:title_author]
51
+ fragment[:author] = $2 || NOTE_DEFAULT_AUTHOR
52
+ fragment.delete(:title_author)
53
+
54
+ # 提取片段的类型与创建日期
55
+ /(#{NOTE_LABEL}|#{HIGHLIGHT_LABEL}).*(#{ADD_LABEL})(.*)/ =~ (fragment[:type_date])
56
+ fragment[:type] = $1
57
+ fragment[:date] = $3 || NOTE_DEFAULT_DATETIME
58
+ fragment.delete(:type_date)
59
+
60
+ fragment[:content].lstrip!
61
+ # 将片段添加到数组
62
+ fragments.push(fragment)
63
+ end
64
+
65
+ return fragments
66
+ end
67
+
68
+ # 解析为笔记
69
+ def parseForNote(fragments = nil)
70
+ fragments ||= parseForFragment()
71
+ @notes = {}
72
+ fragments.each do |fragment|
73
+ title = fragment[:title].strip
74
+ author = fragment[:author]
75
+ fragment.delete(:title)
76
+ fragment.delete(:author)
77
+
78
+ if @notes.has_key?(title)
79
+ @notes[title][:fragment].push(fragment)
80
+ else
81
+ @notes[title] = {
82
+ :title=>title,
83
+ :author=>author,
84
+ :fragment =>[fragment]
85
+ }
86
+ end
87
+ end
88
+
89
+ return @notes
90
+ end
91
+ end
92
+ end
93
+
94
+
95
+
@@ -0,0 +1,3 @@
1
+ module Clippings
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clippings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - badtudou
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Parse notes and highlights in your Kindle clippings, the name of the
42
+ Kindle clipper file was 'My Clippings.txt'
43
+ email:
44
+ - badtudou@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - clippings.gemspec
57
+ - lib/clippings.rb
58
+ - lib/clippings/version.rb
59
+ homepage: https://github.com/BadTudou/clippings
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ allowed_push_host: https://rubygems.org
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.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Parse notes and highlights in your Kindle clippings
84
+ test_files: []