ever_exp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 79f47d4614b6d8709aacd65065421804fa7f725d
4
+ data.tar.gz: 53019da3b514fbb881d9400b75c01f448194bfd9
5
+ SHA512:
6
+ metadata.gz: 4d30d18bc60ac346627b524405195a2d6135cd5022af32891a7fe9b874862fff98acb198dfefc7fa61875cb7d09bf0d5680c4fcc33be40a0c0fb4898d8af959d
7
+ data.tar.gz: 543574f555f5afd3e1e444ea6ef6a54c5927ea4dff7de6cbbe08a1ad3153c3554c397cf0a4505f50f16b35ecfddac8e44ee298dac12c37835a3be20eab73fc93
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
11
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at block24block@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ever_exp.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 ken
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.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # EverExp
2
+
3
+ parse export-evernote-to-individual-html-files
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ever_exp'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ever_exp
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ notes = EverExp::Notes.new 'path/to/dir_storing_exported'
25
+ ```
26
+
27
+ ## License
28
+
29
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
30
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'ever_exp'
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
data/bin/setup ADDED
@@ -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
data/ever_exp.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ever_exp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ever_exp'
8
+ spec.version = EverExp::VERSION
9
+ spec.authors = ['ken']
10
+ spec.email = ['block24block@gmail.com']
11
+
12
+ spec.summary = 'parse files exported from evernote'
13
+ spec.homepage = 'https://github.com/turnon/ever_exp'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.13'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'minitest', '~> 5.0'
26
+ spec.add_dependency 'nokogiri', '~> 1.6'
27
+ end
@@ -0,0 +1,46 @@
1
+ module EverExp
2
+ class Files
3
+
4
+ attr_accessor :note
5
+
6
+ include Enumerable
7
+
8
+ def initialize path
9
+ @path = path
10
+ end
11
+
12
+ def name
13
+ File.basename(@path).gsub(/_files$/, '')
14
+ end
15
+
16
+ def each &block
17
+ _files.each &block
18
+ end
19
+
20
+ def isHtml?
21
+ false
22
+ end
23
+
24
+ private
25
+
26
+ def _files
27
+ @files ||= Dir.new(@path).
28
+ reject { |name| name == '.' or name == '..' }.
29
+ map { |basename| FileInFiles.new File.join(@path, basename) }
30
+ end
31
+
32
+ class FileInFiles
33
+
34
+ attr_reader :location
35
+
36
+ def initialize path
37
+ @location = path
38
+ end
39
+
40
+ def name
41
+ File.basename location
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,65 @@
1
+ module EverExp
2
+ class Html
3
+ class Heading
4
+ def initialize
5
+ @h = Title.new '999', 'heading'
6
+ end
7
+
8
+ def add level, title
9
+ subtitle = Title.new level, title
10
+ last_st = h.last_subtitle
11
+ title = h
12
+ while true do
13
+ if last_st.nil? or last_st.level == subtitle.level
14
+ title.add_sub subtitle
15
+ return
16
+ end
17
+ title = last_st
18
+ last_st = last_st.last_subtitle
19
+ end
20
+ end
21
+
22
+ def to_h
23
+ h.subtitles.map(&:to_h)
24
+ end
25
+
26
+ class Title
27
+
28
+ attr_reader :level, :title, :subtitles
29
+ attr_accessor :parent_title
30
+
31
+ def initialize lvl, ti
32
+ @level = lvl
33
+ @title = ti
34
+ @subtitles = []
35
+ end
36
+
37
+ def last_subtitle
38
+ subtitles.last
39
+ end
40
+
41
+ def add_sub node
42
+ node.parent_title = self
43
+ subtitles << node
44
+ end
45
+
46
+ def add_sib node
47
+ parent_title.add_sub node
48
+ end
49
+
50
+ def to_h
51
+ return title if subtitles.empty?
52
+ {title => subtitles.map(&:to_h)}
53
+ end
54
+
55
+ end
56
+
57
+ private
58
+
59
+ def h
60
+ @h
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,113 @@
1
+ require 'nokogiri'
2
+ require 'ever_exp/heading'
3
+ require 'ever_exp/nokogiri_xml_element'
4
+
5
+ module EverExp
6
+ class Html
7
+
8
+ attr_reader :html, :location
9
+ attr_writer :note
10
+
11
+ def initialize path
12
+ @html = File.open(path) { |f| Nokogiri::HTML(f) }
13
+ @location = path
14
+ end
15
+
16
+ def name
17
+ File.basename(location).gsub(/\.html$/, '')
18
+ end
19
+
20
+ def isHtml?
21
+ true
22
+ end
23
+
24
+ def title
25
+ @title ||= html.title
26
+ end
27
+
28
+ def tags
29
+ parse_meta
30
+ @tags
31
+ end
32
+
33
+ def created
34
+ parse_meta
35
+ @created
36
+ end
37
+
38
+ def updated
39
+ parse_meta
40
+ @updated
41
+ end
42
+
43
+ def code_blocks
44
+ html.
45
+ css('div').
46
+ select { |div| div.attr('style') =~ /^-en-codeblock/ }
47
+ end
48
+
49
+ def imgs
50
+ html.css('img')
51
+ end
52
+
53
+ def content
54
+ _content.to_html
55
+ end
56
+
57
+ def heading?
58
+ not heading.empty?
59
+ end
60
+
61
+ def heading
62
+ return @heading if @heading
63
+ h = Heading.new
64
+ heading_elements.each do |b|
65
+ level, title = level_title b
66
+ h.add level, title
67
+ end
68
+ @heading = h.to_h
69
+ end
70
+
71
+ def heading_elements
72
+ _content.css('div > b, div > span > b')
73
+ end
74
+
75
+ private
76
+
77
+ def level_title bold_tag
78
+ level = (bold_tag.parent.name == 'span' ? bold_tag.parent.attr('style').gsub(/[^\d]/, '') : '1')
79
+ title = bold_tag.text
80
+ [level, title]
81
+ end
82
+
83
+ def _content
84
+ html.css('body > div').last
85
+ end
86
+
87
+ def parse_meta
88
+ return if @parsed_meta
89
+ metas = meta.css('tr')
90
+ raise IOError, 'meta is broken' if metas.size < 2
91
+ @created = parse_date meta_value metas[0]
92
+ @updated = parse_date meta_value metas[1]
93
+ @tags = meta_value metas[2]
94
+ @parsed_meta = true
95
+ end
96
+
97
+ def meta
98
+ @meta ||= html.at_css 'table'
99
+ end
100
+
101
+ def meta_value tr
102
+ tr.css('td').last.at_css('i').text
103
+ end
104
+
105
+ def parse_date date
106
+ date_arr = date.split(/\s+/)
107
+ ymd, hmin = date_arr.first, date_arr.last
108
+ y, m, d = ymd.split('/')
109
+ h, min = hmin.split(':')
110
+ Time.new y, m, d, h, min
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,18 @@
1
+ class Nokogiri::XML::Element
2
+
3
+ def plain_code
4
+ raise NoMethodError, "#{self} is not a code block" unless self['style'] =~ /^-en-codeblock/
5
+ css('div').
6
+ map{ |line| _plain_code line }.
7
+ join("\n")
8
+ end
9
+
10
+ private
11
+
12
+ def _plain_code line
13
+ innerHtml = line.inner_html
14
+ no_nbsp = innerHtml.gsub("\u00A0", ' ')
15
+ no_escape = CGI.unescapeHTML no_nbsp
16
+ br_as_blank_line = (no_escape == '<br>' ? '' : no_escape)
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ require 'ever_exp/without_files'
2
+
3
+ module EverExp
4
+ class Note
5
+
6
+ attr_reader :html
7
+
8
+ def title
9
+ html.title
10
+ end
11
+
12
+ def files
13
+ @files || EverExp::WithoutFiles.instance
14
+ end
15
+
16
+ def tags
17
+ html.tags
18
+ end
19
+
20
+ def tag_array
21
+ tags.split ', '
22
+ end
23
+
24
+ def <<(html_or_files)
25
+ if html_or_files.isHtml?
26
+ @html = html_or_files
27
+ else
28
+ @files = html_or_files
29
+ end
30
+ html_or_files.note = self
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,49 @@
1
+ require 'ever_exp/html'
2
+ require 'ever_exp/files'
3
+ require 'ever_exp/note'
4
+ require 'delegate'
5
+
6
+ module EverExp
7
+ class Notes < DelegateClass(Array)
8
+ def initialize dir
9
+ group_into_notes files_in dir
10
+ super notes_hash.values
11
+ remove_instance_variable :@notes_hash
12
+ end
13
+
14
+ def group_by_tag
15
+ tag_groups = Hash.new { |hash, tag| hash[tag] = [] }
16
+ each do |note|
17
+ note.tag_array.each do |tag|
18
+ tag_groups[tag] << note
19
+ end
20
+ end
21
+ tag_groups
22
+ end
23
+
24
+ private
25
+
26
+ def files_in dir
27
+ wildcast = File.join dir, '*'
28
+ Dir[wildcast]
29
+ end
30
+
31
+ def group_into_notes files
32
+ files.each do |file|
33
+ html_or_files = classify file
34
+ note = notes_hash[html_or_files.name]
35
+ note << html_or_files
36
+ end
37
+ end
38
+
39
+ def classify file
40
+ klass = File.extname(file) == '.html' ? EverExp::Html : EverExp::Files
41
+ klass.new file
42
+ end
43
+
44
+ def notes_hash
45
+ @notes_hash ||= Hash.new { |hash, name| hash[name] = EverExp::Note.new }
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module EverExp
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,27 @@
1
+ module EverExp
2
+ class WithoutFiles
3
+
4
+ include Enumerable
5
+
6
+ class << self
7
+ private :new
8
+
9
+ def instance
10
+ @single ||= new
11
+ end
12
+ end
13
+
14
+ def name
15
+ 'without_files'
16
+ end
17
+
18
+ def each &block
19
+ [].each &block
20
+ end
21
+
22
+ def isHtml?
23
+ false
24
+ end
25
+
26
+ end
27
+ end
data/lib/ever_exp.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'ever_exp/version'
2
+ require 'ever_exp/notes'
3
+
4
+ module EverExp
5
+ # Your code goes here...
6
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ever_exp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ken
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-18 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ description:
70
+ email:
71
+ - block24block@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - ever_exp.gemspec
86
+ - lib/ever_exp.rb
87
+ - lib/ever_exp/files.rb
88
+ - lib/ever_exp/heading.rb
89
+ - lib/ever_exp/html.rb
90
+ - lib/ever_exp/nokogiri_xml_element.rb
91
+ - lib/ever_exp/note.rb
92
+ - lib/ever_exp/notes.rb
93
+ - lib/ever_exp/version.rb
94
+ - lib/ever_exp/without_files.rb
95
+ homepage: https://github.com/turnon/ever_exp
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.8
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: parse files exported from evernote
119
+ test_files: []