chrome2markdown 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/chrome2markdown.rb +74 -0
  3. metadata +59 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 011977a407f74abf4fed0a602cee62804478ecd4
4
+ data.tar.gz: 6ea548ce1b89dd6547df5af360e7a021f00a4a8f
5
+ SHA512:
6
+ metadata.gz: 02b1a43c78db189a8f396f497b8ac6e4b9d1fceaa9bd81fc46ac94c12c2cb3d9999ae21b5bf7122267d6247555fe104298679fc57a414ebbc9e4256bfd40d451
7
+ data.tar.gz: e4e7244df813b9da63e86ad5286dbad906499f9f64e59bd50da80795429058ccdfaaf52a5c183db4e23ecada2ef63d7f71be406e9ebdea0e93db4199837d22ea
@@ -0,0 +1,74 @@
1
+ # TODO:
2
+ # - Shrink functions
3
+ # - Unit tests
4
+ # - Refactor
5
+
6
+ require 'nokogiri'
7
+
8
+ class Chrome2Markdown
9
+ attr_reader :page, :links, :markdown
10
+
11
+ def initialize(bookmarks, folder_name)
12
+ if bookmarks == nil or folder_name == nil
13
+ throw 'Bookmarks file or folder name not specified.'
14
+ end
15
+
16
+ @bookmarks = bookmarks
17
+ @folder_name = folder_name
18
+
19
+ begin
20
+ @page = search_tree(@bookmarks, @folder_name)
21
+ @links = find_links(@page)
22
+ @markdown = to_markdown(@links)
23
+ rescue
24
+ throw 'Could not parse bookmarks.'
25
+ end
26
+ end
27
+
28
+ def search_tree(bookmarks, folder_name)
29
+ page = Nokogiri::HTML(open(bookmarks))
30
+
31
+ if page == nil
32
+ throw 'Bookmarks file is invalid.'
33
+ end
34
+
35
+ page.xpath('//dl/dl').each do |folder|
36
+ if folder.previous_sibling.text.strip == folder_name
37
+ return folder.inner_html.split('<dt>')
38
+ end
39
+ end
40
+ end
41
+
42
+ def find_links(page)
43
+ links = []
44
+
45
+ page.each do |line|
46
+ if line.strip!.include?('<a href')
47
+ # Final line is very messy.
48
+ # Nokogiri adjusts for missing </DT> tags.
49
+ links << line.split(/\r\n/).first
50
+ end
51
+ end
52
+
53
+ # Breaks if not explicitly returned.
54
+ # Might have something to do with array shift (<<).
55
+ links
56
+ end
57
+
58
+ def to_markdown(links)
59
+ markdown = []
60
+
61
+ links.each do |link|
62
+ link.gsub! /\<a href=\"(.*)\" add_date(.*)\>(.*)\<\/a\>/, '[\3](\1)'
63
+
64
+ # HTML entities
65
+ link.gsub! /&amp;/, '&'
66
+ link.gsub! /&lt;/, '<'
67
+ link.gsub! /&rt;/, '>'
68
+
69
+ markdown << link
70
+ end
71
+
72
+ markdown
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chrome2markdown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kirk Elifson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ description: Converts specified folder of links from exported Google Chrome bookmarks
28
+ into markdown.
29
+ email: kirk@parodybit.net
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/chrome2markdown.rb
35
+ homepage: https://rubygems.org/gems/chrome2markdown
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.4.3
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Chrome 2 Markdown
59
+ test_files: []