lei 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.
@@ -0,0 +1,67 @@
1
+ puts "\tController: Content"
2
+
3
+ class ContentController < Sinatra::Base
4
+
5
+ set :content_dir, "#{$root}/content"
6
+ set :stylesheet, "content"
7
+ set :public_folder, $assets_root
8
+ set :views, $views
9
+
10
+ get "/" do
11
+ content = ContentHelpers.get_content(settings.content_dir)
12
+ redirect 404 if content.length == 0
13
+
14
+ url = request.url
15
+
16
+ ContentHelpers.time_sort(content)
17
+ contentParts = ContentHelpers.paginate(content, params, url)
18
+
19
+ slim :content, locals: {
20
+ **contentParts,
21
+ style: ContentHelpers.load_css(settings.stylesheet),
22
+ title: "Posts",
23
+ url: url
24
+ }
25
+ end
26
+
27
+ get "/post/:title" do
28
+ title = params["title"]
29
+ redirect 404 if title.nil? || title.length == 0
30
+
31
+ content = ContentHelpers.get_post(settings.content_dir, title)
32
+ redirect 404 if content.length != 1
33
+
34
+ parsed_title = title.split("-").join(" ")
35
+
36
+ slim :post, locals: {
37
+ content: ContentHelpers.parse_md(content)[0],
38
+ style: ContentHelpers.load_css(settings.stylesheet),
39
+ title: parsed_title,
40
+ url: request.url
41
+ }
42
+ end
43
+
44
+ get "/filter" do
45
+ content = ContentHelpers.get_content(settings.content_dir)
46
+
47
+ ContentHelpers.filter_content(content, params)
48
+ redirect 404 if content.length == 0
49
+
50
+ url = request.url
51
+
52
+ ContentHelpers.time_sort(content)
53
+ contentParts = ContentHelpers.paginate(content, params, url)
54
+
55
+ slim :search_results, locals: {
56
+ **contentParts,
57
+ style: ContentHelpers.load_css(settings.stylesheet),
58
+ title: "Filtered Results",
59
+ url: url
60
+ }
61
+ end
62
+
63
+ not_found do
64
+ slim :notfound, locals: { **ContentHelpers.nf_404, url: request.url }
65
+ end
66
+
67
+ end
@@ -0,0 +1,33 @@
1
+ puts "\tController: Custom"
2
+
3
+ class CustomController < Sinatra::Base
4
+
5
+ set :public_folder, $assets_root
6
+ set :views, $views
7
+
8
+ routes = YAML.load_file("#{$root}/customlist.yml")
9
+
10
+ routes.each do |route|
11
+ name = route["name"]
12
+ path = route["path"]
13
+ puts "\t\t#{path}: #{name}"
14
+
15
+ get path do
16
+ folder = "#{$root}/#{name}"
17
+
18
+ content = route["content"].map { |section| "#{folder}/#{section}" }
19
+
20
+ slim name.to_sym, locals: {
21
+ content: ContentHelpers.parse_md(content),
22
+ style: ContentHelpers.load_css(name),
23
+ title: route["title"],
24
+ url: request.url
25
+ }
26
+ end
27
+ end
28
+
29
+ not_found do
30
+ slim :notfound, locals: { **ContentHelpers.nf_404, url: request.url }
31
+ end
32
+
33
+ end
@@ -0,0 +1,12 @@
1
+ require "#{$root}/controllers/content"
2
+
3
+ puts "\tController: Test"
4
+
5
+ class TestController < ContentController
6
+
7
+ set :content_dir, "#{$root}/test"
8
+ set :stylesheet, "content"
9
+ set :public_folder, $assets_root
10
+ set :views, $views
11
+
12
+ end
@@ -0,0 +1,131 @@
1
+ puts "\tHelper: Content"
2
+
3
+ require "redcarpet"
4
+
5
+ module ContentHelpers
6
+
7
+ CARPET = Redcarpet::Markdown.new(
8
+ Redcarpet::Render::HTML.new({
9
+ hard_wrap: true,
10
+ highlight: true
11
+ })
12
+ )
13
+
14
+ def self.filter_content(content, params)
15
+ term = params["term"]
16
+ if !term.nil? && term.length > 2
17
+ content.reject! do |c|
18
+ post = `cat #{c}`
19
+ !post.match?(Regexp.new(term, true))
20
+ end
21
+ end
22
+
23
+ yyyy = params["year"]
24
+ if !yyyy.nil? && yyyy.length == 4
25
+ content.reject! { |c| File::Stat.new(c).mtime.year != yyyy.to_i }
26
+ end
27
+
28
+ mm = params["month"]
29
+ if !mm.nil? && mm.length == 2
30
+ content.reject! { |c| File::Stat.new(c).mtime.month != mm.to_i }
31
+ end
32
+
33
+ dd = params["day"]
34
+ if !dd.nil? && dd.length == 2
35
+ content.reject! { |c| File::Stat.new(c).mtime.day != dd.to_i }
36
+ end
37
+ end
38
+
39
+ def self.get_content(contentDir)
40
+ all = Dir.glob("#{contentDir}/*.md")
41
+ classified = YAML.load_file($banlist)
42
+
43
+ all - classified
44
+ end
45
+
46
+ def self.get_post(contentDir, title)
47
+ content = self.get_content(contentDir)
48
+
49
+ post = "#{contentDir}/#{title}.md"
50
+
51
+ content.include?(post) ? [ post ] : []
52
+ end
53
+
54
+ <<~load_css
55
+ This function reads and returns the contents of a CSS file as a string.
56
+ Its return value gets stored in a variable for ease of interpolation in
57
+ templates.
58
+ load_css
59
+
60
+ def self.load_css(filename)
61
+ `cat #{$style_root}/#{filename}.css`
62
+ end
63
+
64
+ def self.mp_eu(path, params, replacement)
65
+ newParams = {}
66
+ params.each { |k, v| newParams[k] = v }
67
+ replacement.each { |i, j| newParams[i] = j }
68
+ encoded = URI.encode_www_form(newParams)
69
+
70
+ "#{path}?#{encoded}"
71
+ end
72
+
73
+ def self.nf_404
74
+ {
75
+ title: "404: Not Found",
76
+ style: self.load_css("notfound")
77
+ }
78
+ end
79
+
80
+ def self.paginate(content, params, url)
81
+ pageParam = params["page"].to_i
82
+
83
+ page = pageParam != 0 ? pageParam : 1
84
+ pages = (content.length / 5.0).ceil
85
+
86
+ firstIndex = (page - 1) * 5
87
+ lastIndex = page * 5 - 1
88
+
89
+ path = URI(url).path
90
+ pageUrls = [ nil, nil, nil, nil ]
91
+
92
+ if page > 1
93
+ pageUrls[0] = self.mp_eu(path, params, { "page" => "1" })
94
+
95
+ prev = (page - 1).to_s
96
+ pageUrls[1] = self.mp_eu(path, params, { "page" => prev })
97
+ end
98
+
99
+ if page < pages
100
+ foll = (page + 1).to_s
101
+ pageUrls[2] = self.mp_eu(path, params, { "page" => foll })
102
+
103
+ pageUrls[3] = self.mp_eu(path, params, { "page" => pages.to_s })
104
+ end
105
+
106
+ {
107
+ content: self.parse_md(content[firstIndex..lastIndex]),
108
+ page: page,
109
+ pages: pages,
110
+ pageUrls: pageUrls
111
+ }
112
+ end
113
+
114
+ <<~parse_md
115
+ This function takes a list of filenames, reads their contents, and
116
+ utilizes the Redcarpet gem to parse them into HTML.
117
+ parse_md
118
+
119
+ def self.parse_md(filenames)
120
+ filenames.map do |filename|
121
+ content = `cat #{filename}`
122
+ CARPET.render(content)
123
+ end
124
+ end
125
+
126
+ def self.time_sort(content)
127
+ content.sort_by! { |c| File::Stat.new(c).mtime }
128
+ content.reverse!
129
+ end
130
+
131
+ end
@@ -0,0 +1,44 @@
1
+ puts "\tHelper: Global Utilities"
2
+
3
+ require "dotenv"
4
+
5
+ module GlobalUtils
6
+
7
+ def self.declare_globals
8
+ puts "\t\t--- Loading Environment Variables ---"
9
+
10
+ Dotenv.load
11
+
12
+ puts "\t\t--- Declaring Globals ---"
13
+
14
+ $assets_root = "#{$root}/static"
15
+ $style_root = "#{$assets_root}/styles"
16
+ $views = "#{$root}/views"
17
+
18
+ $banlist = "#{$root}/banlist.yml"
19
+
20
+ <<~AMP
21
+ AMP Static Header Parts, as of 15 APR 2018:
22
+ https://www.ampproject.org/docs/fundamentals/spec
23
+
24
+ AMP Boilerplate, as of 15 APR 2018:
25
+ https://www.ampproject.org/docs/fundamentals/spec/amp-boilerplate
26
+
27
+ amp-bind, as of 15 APR 2018:
28
+ https://www.ampproject.org/docs/reference/components/amp-bind
29
+ AMP
30
+
31
+ $amp_static_header = <<~HEADER
32
+ <meta charset="utf-8">
33
+ <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
34
+ <script async src="https://cdn.ampproject.org/v0.js"></script>
35
+ HEADER
36
+
37
+ $amp_boiler = <<~BOILER
38
+ <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
39
+ BOILER
40
+
41
+ $amp_bind = "<script async custom-element=\"amp-bind\" src=\"https://cdn.ampproject.org/v0/amp-bind-0.1.js\"></script>"
42
+ end
43
+
44
+ end
@@ -0,0 +1,100 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+ RSpec.configure do |config|
17
+ # rspec-expectations config goes here. You can use an alternate
18
+ # assertion/expectation library such as wrong or the stdlib/minitest
19
+ # assertions if you prefer.
20
+ config.expect_with :rspec do |expectations|
21
+ # This option will default to `true` in RSpec 4. It makes the `description`
22
+ # and `failure_message` of custom matchers include text for helper methods
23
+ # defined using `chain`, e.g.:
24
+ # be_bigger_than(2).and_smaller_than(4).description
25
+ # # => "be bigger than 2 and smaller than 4"
26
+ # ...rather than:
27
+ # # => "be bigger than 2"
28
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29
+ end
30
+
31
+ # rspec-mocks config goes here. You can use an alternate test double
32
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
33
+ config.mock_with :rspec do |mocks|
34
+ # Prevents you from mocking or stubbing a method that does not exist on
35
+ # a real object. This is generally recommended, and will default to
36
+ # `true` in RSpec 4.
37
+ mocks.verify_partial_doubles = true
38
+ end
39
+
40
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41
+ # have no way to turn it off -- the option exists only for backwards
42
+ # compatibility in RSpec 3). It causes shared context metadata to be
43
+ # inherited by the metadata hash of host groups and examples, rather than
44
+ # triggering implicit auto-inclusion in groups with matching metadata.
45
+ config.shared_context_metadata_behavior = :apply_to_host_groups
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ =begin
50
+ # This allows you to limit a spec run to individual examples or groups
51
+ # you care about by tagging them with `:focus` metadata. When nothing
52
+ # is tagged with `:focus`, all examples get run. RSpec also provides
53
+ # aliases for `it`, `describe`, and `context` that include `:focus`
54
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55
+ config.filter_run_when_matching :focus
56
+
57
+ # Allows RSpec to persist some state between runs in order to support
58
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
59
+ # you configure your source control system to ignore this file.
60
+ config.example_status_persistence_file_path = "spec/examples.txt"
61
+
62
+ # Limits the available syntax to the non-monkey patched syntax that is
63
+ # recommended. For more details, see:
64
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
65
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
66
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
67
+ config.disable_monkey_patching!
68
+
69
+ # This setting enables warnings. It's recommended, but in some cases may
70
+ # be too noisy due to issues in dependencies.
71
+ config.warnings = true
72
+
73
+ # Many RSpec users commonly either run the entire suite or an individual
74
+ # file, and it's useful to allow more verbose output when running an
75
+ # individual spec file.
76
+ if config.files_to_run.one?
77
+ # Use the documentation formatter for detailed output,
78
+ # unless a formatter has already been configured
79
+ # (e.g. via a command-line flag).
80
+ config.default_formatter = "doc"
81
+ end
82
+
83
+ # Print the 10 slowest examples and example groups at the
84
+ # end of the spec run, to help surface which specs are running
85
+ # particularly slow.
86
+ config.profile_examples = 10
87
+
88
+ # Run specs in random order to surface order dependencies. If you find an
89
+ # order dependency and want to debug it, you can fix the order by providing
90
+ # the seed, which is printed after each run.
91
+ # --seed 1234
92
+ config.order = :random
93
+
94
+ # Seed global randomization in this process using the `--seed` CLI option.
95
+ # Setting this allows you to use `--seed` to deterministically reproduce
96
+ # test failures related to randomization by passing the same `--seed` value
97
+ # as the one that triggered the failure.
98
+ Kernel.srand config.seed
99
+ =end
100
+ end
@@ -0,0 +1,176 @@
1
+ require "yaml"
2
+
3
+ root = File.dirname(__FILE__)
4
+ fixtures = "#{root}/../fixtures"
5
+ $banlist = "#{fixtures}/banlist.yml"
6
+ $style_root = fixtures
7
+
8
+ require_relative "../../helpers/content_helpers"
9
+
10
+ RSpec.describe "Tests Module: ContentHelpers" do
11
+
12
+ content = ContentHelpers.get_content(fixtures)
13
+
14
+ it "Tests variable $banlist: Verifies exclusion" do
15
+ expect(content.length).not_to eq 7
16
+ expect(content).not_to include "#{fixtures}/md-2.md"
17
+ end
18
+
19
+ it "Tests method time_sort: Correctly sorts by mtime" do
20
+ ContentHelpers.time_sort(content)
21
+
22
+ expect(content.first).to eq "#{fixtures}/md-1.md"
23
+ expect(content.last).to eq "#{fixtures}/md-3.md"
24
+ end
25
+
26
+ just6 = []
27
+ for i in 1..7
28
+ just6 << "#{fixtures}/md-#{i}.md" if i != 2
29
+ end
30
+ ContentHelpers.time_sort(just6)
31
+
32
+ it "Tests method get_content: Correctly retrieves content" do
33
+ expect(content.length).to eq 6
34
+ expect(content).to eq just6
35
+ end
36
+
37
+ it "Tests method get_post: Correctly retrieves post" do
38
+ post = ContentHelpers.get_post(fixtures, "md-4")
39
+ ContentHelpers.time_sort(post)
40
+
41
+ expect(post.length).to eq 1
42
+ expect(post).to eq [ "#{fixtures}/md-4.md" ]
43
+ end
44
+
45
+ path = "https://www.testDomain.com/fake"
46
+
47
+ it "Tests method mp_eu: Correctly adds a param" do
48
+ params = { "mode" => "test" }
49
+ newParams = { "action" => "add" }
50
+ newUrl = ContentHelpers.mp_eu(path, params, newParams)
51
+
52
+ expect(newUrl).not_to eq path
53
+ expect(newUrl).not_to eq "#{path}?mode=test"
54
+ expect(newUrl).to eq "#{path}?mode=test&action=add"
55
+ end
56
+
57
+ it "Tests method mp_eu: Correctly replaces a param" do
58
+ params = { "action" => "add", "mode" => "test" }
59
+ newParams = { "action" => "remove" }
60
+ newUrl = ContentHelpers.mp_eu(path, params, newParams)
61
+
62
+ expect(newUrl).not_to eq "#{path}?action=add&mode=test"
63
+ expect(newUrl).to eq "#{path}?action=remove&mode=test"
64
+ end
65
+
66
+ it "Tests method paginate: Correctly retrieves first page" do
67
+ params = {}
68
+ paginated = ContentHelpers.paginate(just6, params, path)
69
+
70
+ expect(paginated[:page]).to eq 1
71
+ expect(paginated[:pages]).to eq 2
72
+ expect(paginated[:content].length).to eq 5
73
+ expect(paginated[:pageUrls]).to eq [
74
+ nil,
75
+ nil,
76
+ "/fake?page=2",
77
+ "/fake?page=2"
78
+ ]
79
+
80
+ params2 = { "page" => "1" }
81
+ paginated2 = ContentHelpers.paginate(just6, params2, path)
82
+
83
+ expect(paginated2[:page]).to eq 1
84
+ expect(paginated2[:pages]).to eq 2
85
+ expect(paginated2[:content].length).to eq 5
86
+ expect(paginated2[:pageUrls]).to eq [
87
+ nil,
88
+ nil,
89
+ "/fake?page=2",
90
+ "/fake?page=2"
91
+ ]
92
+ end
93
+
94
+ it "Tests method paginate: Correctly retrieves second page" do
95
+ params = { "page" => "2" }
96
+ paginated = ContentHelpers.paginate(just6, params, path)
97
+
98
+ expect(paginated[:page]).to eq 2
99
+ expect(paginated[:pages]).to eq 2
100
+ expect(paginated[:content].length).to eq 1
101
+ expect(paginated[:pageUrls]).to eq [
102
+ "/fake?page=1",
103
+ "/fake?page=1",
104
+ nil,
105
+ nil
106
+ ]
107
+ end
108
+
109
+ it "Tests method filter_content: Correctly filters on term" do
110
+ params = { "term" => "third" }
111
+ filtered = just6.clone
112
+ ContentHelpers.filter_content(filtered, params)
113
+
114
+ expect(filtered.length).to eq 1
115
+ expect(filtered[0]).to eq "#{fixtures}/md-3.md"
116
+ end
117
+
118
+ it "Tests method filter_content: Correctly filters on year" do
119
+ params = { "year" => "2018" }
120
+ filtered = just6.clone
121
+ ContentHelpers.filter_content(filtered, params)
122
+
123
+ expect(filtered.length).to eq 6
124
+
125
+ params2 = { "year" => "2017" }
126
+ ContentHelpers.filter_content(filtered, params2)
127
+
128
+ expect(filtered.length).to eq 0
129
+ end
130
+
131
+ it "Tests method filter_content: Correctly filters on month" do
132
+ params = { "year" => "2018", "month" => "05" }
133
+ filtered = just6.clone
134
+ ContentHelpers.filter_content(filtered, params)
135
+
136
+ expect(filtered.length).to eq 6
137
+
138
+ params2 = { "year" => "2018", "month" => "04" }
139
+ ContentHelpers.filter_content(filtered, params2)
140
+
141
+ expect(filtered.length).to eq 0
142
+ end
143
+
144
+ it "Tests method filter_content: Correctly filters on day" do
145
+ params = { "year" => "2018", "month" => "05", "day" => "03" }
146
+ filtered = just6.clone
147
+ ContentHelpers.filter_content(filtered, params)
148
+
149
+ expect(filtered.length).to eq 4
150
+
151
+ params2 = { "year" => "2018", "month" => "05", "day" => "10" }
152
+ filtered2 = just6.clone
153
+ ContentHelpers.filter_content(filtered2, params2)
154
+
155
+ expect(filtered2.length).to eq 1
156
+
157
+ params3 = { "year" => "2018", "month" => "05", "day" => "04" }
158
+ filtered3 = just6.clone
159
+ ContentHelpers.filter_content(filtered3, params3)
160
+
161
+ expect(filtered3.length).to eq 1
162
+ end
163
+
164
+ it "Tests method parse_md: Correctly generates HTML from Markdown" do
165
+ converted = ContentHelpers.parse_md([ "#{fixtures}/md-1.md" ])
166
+
167
+ expect(converted[0]).to eq "<h1>Markdown 1st</h1>\n"
168
+ end
169
+
170
+ it "Tests method load_css: Correctly returns CSS file contents" do
171
+ processed = ContentHelpers.load_css("test")
172
+
173
+ expect(processed).to eq ".mock{display: none;}"
174
+ end
175
+
176
+ end