ramble 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
  SHA1:
3
- metadata.gz: 1c72f6ed32b84d676a50403f37d626dd20cb31a6
4
- data.tar.gz: 671f2fc57d4db54104f5c369c0f0412cc15b5acd
3
+ metadata.gz: d95f1c5c7311ad5429e2fedb10583181c4c3df71
4
+ data.tar.gz: 2c1249eb1a7d24ac50ba72b7fddfecb7cf3c30a5
5
5
  SHA512:
6
- metadata.gz: 091614c873e7f9e3034a86e92ea7a22840cc426a2ecf09875ac52f9982605d82357a500ea26d1871fc87dcbe42e7e9ce486fbf46e675200fe53b833abc8711e4
7
- data.tar.gz: 45d90bc1f049edf31d1b1afd5db5f9f22d737a2ae5acbcfcff342761cdcf647741c72165c1ad49ac8f95671027ed13cc4bbd11ef43efad26b0ec94fe855978b8
6
+ metadata.gz: 27e5aabff6d409dc6d49194a2749b38e4e2661559a8548c43af08383490da3529ab38e4c65aada4e41e167244cf2d19c2d770a8b001c34cafd27d36b1b644455
7
+ data.tar.gz: 463fc201bb724e4c4b6f40b657e66553014d301cd479bd84123bfbdf016784cbbb67e230f9e33f65769b84cac3973e1419d18a325fd3953dccab8c18c633fbea
data/lib/ramble.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "ramble/version"
2
+ require "ramble/blog_post"
3
+ require "ramble/blog_post/markdown_parser"
2
4
 
3
5
  module Ramble
4
- # Your code goes here...
5
6
  end
@@ -0,0 +1,61 @@
1
+ require "ramble/blog_post/markdown_parser"
2
+
3
+ module Ramble
4
+ class BlogPost
5
+ class NotFound < StandardError; end
6
+
7
+ attr_reader :file, :title, :body, :preview
8
+
9
+ class << self
10
+ # TODO: Refactor this shit to be better. Currently 47.8 flog score.
11
+ def all(options = { sort_by: :written_on, asc: true })
12
+ Dir.glob("app/blog_posts/*").map do |file_path|
13
+ new(File.open(file_path))
14
+ end.sort do |a, b|
15
+ if options[:asc]
16
+ a.send(options[:sort_by]) <=> b.send(options[:sort_by])
17
+ else
18
+ b.send(options[:sort_by]) <=> a.send(options[:sort_by])
19
+ end
20
+ end
21
+ end
22
+
23
+ def find_by_slug(slug)
24
+ post = all.detect { |blog_post| blog_post.slug == slug }
25
+
26
+ unless post
27
+ raise Ramble::BlogPost::NotFound
28
+ end
29
+
30
+ post
31
+ end
32
+ end
33
+
34
+ def initialize(file)
35
+ @file = file
36
+ @title = parsed_markdown.title
37
+ @body = parsed_markdown.body
38
+ @preview = parsed_markdown.preview
39
+ end
40
+
41
+ def slug
42
+ underscored_slug = file_name.match(/\d+_(.*)/)[1]
43
+ underscored_slug.gsub("_", "-")
44
+ end
45
+
46
+ def written_on
47
+ timestamp = file_name.match(/(\d+)_/)[1]
48
+ Date.parse(timestamp)
49
+ end
50
+
51
+ private
52
+
53
+ def file_name
54
+ @file_name ||= File.basename(file, ".md")
55
+ end
56
+
57
+ def parsed_markdown
58
+ @parsed_markdown ||= BlogPost::MarkdownParser.new(file.read)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,56 @@
1
+ require "nokogiri"
2
+ require "redcarpet"
3
+
4
+ module Ramble
5
+ class BlogPost
6
+ class MarkdownParser
7
+ attr_reader :title, :body, :preview
8
+
9
+ def initialize(contents_of_file)
10
+ @contents_of_file = contents_of_file
11
+ @title = parsed_title
12
+ @body = parsed_body
13
+ @preview = parsed_preview
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :contents_of_file
19
+
20
+ def document
21
+ @document ||= Nokogiri::HTML.parse(raw_html)
22
+ end
23
+
24
+ def parsed_title
25
+ return @parsed_title if @parsed_title
26
+ heading = document.search("//h1").first.remove
27
+ @parsed_title = heading.to_html.match(/<h1>(.*)<\/h1>/)[1]
28
+ end
29
+
30
+ def parsed_body
31
+ document.search("//body").children.to_html
32
+ end
33
+
34
+ def parsed_preview
35
+ document.search("//p").first.text
36
+ end
37
+
38
+ def raw_html
39
+ Redcarpet::Markdown.new(renderer, extensions).
40
+ render(contents_of_file)
41
+ end
42
+
43
+ def render_options
44
+ { hard_wrap: true }
45
+ end
46
+
47
+ def renderer
48
+ @renderer ||= Redcarpet::Render::HTML.new(render_options)
49
+ end
50
+
51
+ def extensions
52
+ { fenced_code_blocks: true }
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module Ramble
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
data/ramble.gemspec CHANGED
@@ -20,6 +20,9 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
+ spec.add_runtime_dependency "nokogiri"
24
+ spec.add_runtime_dependency "redcarpet"
25
+
23
26
  spec.add_development_dependency "bundler", "~> 1.12"
24
27
  spec.add_development_dependency "rake", "~> 10.0"
25
28
  spec.add_development_dependency "rspec", "~> 3.0"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ramble
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
  - Alex Stophel
@@ -10,6 +10,34 @@ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2016-07-04 00:00:00.000000000 Z
12
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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redcarpet
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -72,6 +100,8 @@ files:
72
100
  - bin/rspec
73
101
  - bin/setup
74
102
  - lib/ramble.rb
103
+ - lib/ramble/blog_post.rb
104
+ - lib/ramble/blog_post/markdown_parser.rb
75
105
  - lib/ramble/version.rb
76
106
  - ramble.gemspec
77
107
  homepage: https://github.com/alexstophel/ramble