styledown2 2.0.0.pre5

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4eaad569e5408829bb0289e13088fe4638a65d8a
4
+ data.tar.gz: d3c45314499459d4667aa06f911b4a2cf815977d
5
+ SHA512:
6
+ metadata.gz: dbd4d66a476871d50a819bc7e2e942575f1fb04310595ec9f72de7d634f9964a4e6d22ce99b5a89838771f3a114fcac6e72be81be7ba557fb0ae9348274999ac
7
+ data.tar.gz: 74a62fe9d0020462a1f948923fecd32a417987b3558584611db43f12aaa6b7bf55055f6c2291dab6a7237f3a2a3f92724c3914027397278151f27034b07007e3
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /Gemfile.lock
2
+ /pkg
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Styledown for Ruby
2
+
3
+ > Write maintainable CSS styleguides using Markdown
4
+
5
+ Ruby implementation for [Styledown2](https://github.com/styledown/styledown2).
6
+
7
+ ## Install
8
+
9
+ ```
10
+ gem 'styledown2'
11
+ ```
12
+
13
+ ## API
14
+
15
+ See [API reference](docs/api.md) for full API reference.
16
+
17
+ ### Object-oriented API
18
+
19
+ This gem provides a `Styledown` class.
20
+
21
+ ```rb
22
+ styleguide = Styledown.new('/path/to/styleguides')
23
+ styleguide.render # Updates #output
24
+
25
+ styleguide.output
26
+ # => {
27
+ # 'buttons.html' => { 'contents' => '...' },
28
+ # 'forms.html' => { 'contents' => '...' },
29
+ # 'styledown/script.js' => { 'contents' => '...' }
30
+ # }
31
+ ```
32
+
33
+ See [API reference](docs/api.md#styledown) for full reference on the `Styledown` class.
34
+
35
+ ### Functional API
36
+
37
+ This implements the same functional API as the JavaScript `styledown2`.
38
+
39
+ ```rb
40
+ files = Styledown.read(...)
41
+ data = Styledown.parse(files)
42
+ output = Styledown.render(data)
43
+ ```
44
+
45
+ See [API reference](docs/api.md#class-methods) for full reference on `Styledown` class methods.
46
+
47
+ ## Thanks
48
+
49
+ **styledown2-ruby** © 2017+, Rico Sta. Cruz. Released under the [MIT] License.<br>
50
+ Authored and maintained by Rico Sta. Cruz with help from contributors ([list][contributors]).
51
+
52
+ > [ricostacruz.com](http://ricostacruz.com) &nbsp;&middot;&nbsp;
53
+ > GitHub [@rstacruz](https://github.com/rstacruz) &nbsp;&middot;&nbsp;
54
+ > Twitter [@rstacruz](https://twitter.com/rstacruz)
55
+
56
+ [MIT]: http://mit-license.org/
57
+ [contributors]: http://github.com/styledown/styledown2-ruby/contributors
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ task default: :test
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ desc 'Runs tests'
7
+ task :test do
8
+ $:.unshift(File.expand_path(__FILE__, '../lib'))
9
+ Dir['test/**/*test.rb'].each { |fn| require_relative fn }
10
+ end
data/docs/api.md ADDED
@@ -0,0 +1,144 @@
1
+ # API
2
+
3
+ ## Styledown
4
+
5
+ > `styleguide = Styledown.new(path, [options])`
6
+
7
+ Styledown class.
8
+
9
+ ```rb
10
+ styleguide = Styledown.new('/path/to/guides')
11
+ styleguide.render
12
+
13
+ styleguide.output
14
+ # => {
15
+ # 'buttons.html' => { 'contents' => '...' },
16
+ # 'forms.html' => { 'contents' => '...' }
17
+ # }
18
+ ```
19
+
20
+ ### render
21
+
22
+ > `styleguide.render`
23
+
24
+ Processes files and updates [#output](#output). Styledown keeps a cache, so this will not re-read or re-process if no files have changed. See [Styledown class](#styledown) for an example.
25
+
26
+ ### fast_render
27
+
28
+ > `styleguide.fast_render`
29
+
30
+ Like [#render](#render), but skips checking if any files have been updated. Use this in production.
31
+
32
+ ### render!
33
+
34
+ > `styleguide.render!`
35
+
36
+ Forces a [#render](#render) regardless of whether files have changed or not.
37
+
38
+ ### output
39
+
40
+ > `styleguide.output`
41
+
42
+ The final transformed files. This is a Hash. See [Styledown class](#styledown) for an example.
43
+
44
+ ### add\_data\_filter
45
+
46
+ > `styleguide.add_data_filter { |data| ... }`
47
+
48
+ Adds a function to be invoked on [#render](#render) that will transform Styledown raw data. The given block should return a new `data`, or the same one if it was mutated.
49
+
50
+ ### add\_file\_filter
51
+
52
+ > `styleguide.add_file_filter { |filename, file| ... }`
53
+
54
+ Adds a function to be invoked on [#render](#render) that will transform Styledown `file` data. The given block should return a `[filename, file]` tuple.
55
+
56
+ ```rb
57
+ styleguide = Styledown.new('/path/to/guides')
58
+
59
+ styleguide.add_file_filter do |filename, file|
60
+ filename = filename.gsub(/\.html$/, '.htm')
61
+ [filename, file]
62
+ end
63
+
64
+ styleguide.render
65
+ ```
66
+
67
+ ### add\_section\_filter
68
+
69
+ > `styleguide.add_section_filter { |section| ... }`
70
+
71
+ Adds a function to be invoked on [#render](#render) that will transform Styledown `section` data. The given block should return a new `section`, or the same one if it was mutated.
72
+
73
+ ```rb
74
+ styleguide = Styledown.new('/path/to/guides')
75
+
76
+ styleguide.add_section_filter do |section|
77
+ # Prefix id's for whatever reason
78
+ section['id'] = "sg-#{section['id']}"
79
+ section
80
+ end
81
+
82
+ styleguide.render
83
+ ```
84
+
85
+ ### add\_part\_filter
86
+
87
+ > `styleguide.add_part_filter { |part| ... }`
88
+
89
+ Adds a function to be invoked on [#render](#render) that will transform Styledown `part` data. The given block should return a new `part`, or the same one if it was mutated.
90
+
91
+ ```rb
92
+ styleguide = Styledown.new('/path/to/guides')
93
+
94
+ styleguide.add_part_filter do |part|
95
+ part['class'] += ' my-class'
96
+ part
97
+ end
98
+
99
+ styleguide.render
100
+ ```
101
+
102
+ ### add\_figure\_filter
103
+
104
+ > `styleguide.add_figure_filter(language) { |content| ... }`
105
+
106
+ Adds a function to be invoked on [#render](#render) for transpiling example figures. The given block should return `[language, content]` where `language` is the new language it was transformed to, and `content` is the transformed result.
107
+
108
+ ```rb
109
+ styleguide = Styledown.new('/path/to/guides')
110
+
111
+ styleguide.add_figure_filter('erb') do |contetns|
112
+ [ 'html', ERB.render(contents) ]
113
+ end
114
+
115
+ styleguide.render
116
+ ```
117
+
118
+ ### invalidate
119
+
120
+ Invalidates the cache so that the next [#render](#render) will always run.
121
+
122
+ ### invalidate_data
123
+
124
+ Partially invalidates the cache so that the next [#render](#render) will always run the build step. It will not re-run the read step, however.
125
+
126
+ ## Class functions
127
+
128
+ ### Styledown.read
129
+
130
+ > `Styledown.read(path, [options])`
131
+
132
+ Reads files in `path` and returns their contents in a Hash, ready to be processed by [styledown.build](#styledown-build). This mirrors the `styledown.read()` implementation in JavaScript.
133
+
134
+ ### Styledown.build
135
+
136
+ > `Styledown.build(files, [options])`
137
+
138
+ Processes `files` (as returned by [styledown.read](#styledown-read) and returns data to be rendered. This mirrors the `styledown.build()` implementation in JavaScript.
139
+
140
+ ### Styledown.render
141
+
142
+ > `Styledown.render(data, [options])`
143
+
144
+ Processes `data` (as returned by [styledown.build](#styledown-build) and returns output files. This mirrors the `styledown.render()` implementation in JavaScript.
@@ -0,0 +1,26 @@
1
+ class Styledown
2
+ # Functional API for Styledown that interfaces via ExecJS. This is the
3
+ # low-level API that the OOP API uses.
4
+ module ClassMethods
5
+ def context
6
+ @context ||= begin
7
+ require 'execjs'
8
+ ExecJS.compile(Styledown::Source::SOURCE)
9
+ end
10
+ end
11
+
12
+ def build(source, options = {})
13
+ context.call('Styledown.build', source, options)
14
+ end
15
+
16
+ def render(data, options = {})
17
+ context.call('Styledown.render', data, options)
18
+ end
19
+
20
+ # Reimplementation of Styledown.read(). Reads files and returns their
21
+ # contents into a Hash.
22
+ def read(paths, options = {})
23
+ FileReader.read(paths, options)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ class Styledown
2
+ module FileReader
3
+ SEARCH_GLOB = '{styledown.json,**/*.md,templates/**/*.{html,js,css}}'
4
+
5
+ def self.read(paths, options = {})
6
+ glob(paths).inject({}) do |result, (file, short_file)|
7
+ result[short_file] = { 'contents' => File.read(file) }
8
+ result
9
+ end
10
+ end
11
+
12
+ def self.mtime(paths)
13
+ glob(paths).inject(0) do |mtime, (file, _)|
14
+ new_mtime = File.mtime(file).to_i
15
+ new_mtime > mtime ? new_mtime : mtime
16
+ end
17
+ end
18
+
19
+ # Returns a list of files in a `path`. Each item is a tuple of `[full_path,
20
+ # short_path]`.
21
+ #
22
+ # glob('/path/to/foo')
23
+ # => [
24
+ # [ '/path/to/foo/bar.txt', 'bar.txt' ],
25
+ # [ '/path/to/foo/baz.txt', 'baz.txt' ]
26
+ # ]
27
+ #
28
+ def self.glob(paths)
29
+ [*paths].inject([]) do |result, path|
30
+ abspath = File.absolute_path(path)
31
+ glob = File.join(abspath, SEARCH_GLOB)
32
+
33
+ result + Dir[glob].map do |file|
34
+ short_file = file[(abspath.length + File::SEPARATOR.length)..-1]
35
+ .gsub(/#{File::SEPARATOR}/, '/')
36
+
37
+ [file, short_file]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ class Styledown
2
+ VERSION = '2.0.0.pre5'
3
+ end
data/lib/styledown.rb ADDED
@@ -0,0 +1,146 @@
1
+ require 'styledown/class_methods'
2
+ require 'styledown/source'
3
+ require 'styledown/file_reader'
4
+
5
+ class Styledown
6
+ extend Styledown::ClassMethods
7
+
8
+ # You can change these and they will be honored on next #render
9
+ attr_reader :paths
10
+ attr_reader :options
11
+
12
+ # Pipeline artifacts
13
+ attr_reader :mtime # Last modified time
14
+ attr_reader :input # Output of Styledown.read
15
+ attr_reader :raw_data # Output of Styledown.build
16
+ attr_reader :data # Output of apply_data_filters
17
+ attr_reader :output # Output of Styledown.render (final output)
18
+
19
+ # Returns a styleguide context.
20
+ def initialize(paths = nil, options = {})
21
+ @paths = paths
22
+ @options = options
23
+
24
+ @data_filters = []
25
+ end
26
+
27
+ def paths=(paths)
28
+ invalidate
29
+ @paths = paths
30
+ end
31
+
32
+ def options=(options)
33
+ invalidate
34
+ @options = options
35
+ end
36
+
37
+ # Reads files, processes them, and updates the `#output`.
38
+ def render
39
+ invalidate_if_updated
40
+ fast_render
41
+ end
42
+
43
+ # Renders without invalidation.
44
+ def fast_render
45
+ @input ||= Styledown.read(@paths, @options)
46
+ @raw_data ||= Styledown.build(@input, @options)
47
+ @data ||= apply_data_filters(@raw_data)
48
+ @output ||= Styledown.render(@data, @options)
49
+ self
50
+ end
51
+
52
+ # Like `#render`, but always re-runs.
53
+ # Also aliased as `#reload`.
54
+ def render!
55
+ invalidate
56
+ render
57
+ end
58
+
59
+ # Busts the cache
60
+ def invalidate
61
+ @mtime = nil
62
+ @input = nil
63
+ @raw_data = nil
64
+ invalidate_data
65
+ end
66
+
67
+ # Busts the cache, partially
68
+ def invalidate_data
69
+ @data = nil
70
+ @output = nil
71
+ end
72
+
73
+ def valid?
74
+ !!@output
75
+ end
76
+
77
+ def add_data_filter(&blk)
78
+ invalidate_data
79
+ @data_filters << blk
80
+ end
81
+
82
+ # Adds a function that will transform files on `#render`.
83
+ # The given block should return `[filename, file]`.
84
+ def add_file_filter(&blk)
85
+ add_data_filter do |data|
86
+ files = data['files'].map do |filename, file|
87
+ blk.(filename, file) # => [filename, file]
88
+ end
89
+ data['files'] = Hash[files]
90
+ data
91
+ end
92
+ end
93
+
94
+ # Adds a function that will transform sections on `#render`.
95
+ def add_section_filter(&blk)
96
+ add_file_filter do |filename, file|
97
+ file['sections'].map! do |section|
98
+ blk.(section, filename, file)
99
+ end if file['sections']
100
+ [filename, file]
101
+ end
102
+ end
103
+
104
+ # Adds a function that will transform section parts on `#render`.
105
+ def add_part_filter(&blk)
106
+ add_section_filter do |section, filename, file|
107
+ section['parts'].map! do |part|
108
+ res = blk.(part, section, filename, file)
109
+ res
110
+ end if section['parts']
111
+ section
112
+ end
113
+ end
114
+
115
+ # Adds a function that will transform section part figures on `#render`.
116
+ def add_figure_filter(lang, &blk)
117
+ add_part_filter do |part, section, filename, file|
118
+ if part['isExample'] && [*lang].map(&:to_s).include?(part['language'])
119
+ new_lang, new_content = blk.(part['content'])
120
+ part['content'] = new_content
121
+ part['language'] = new_lang
122
+ part
123
+ else
124
+ part
125
+ end
126
+ end
127
+ end
128
+
129
+ private
130
+
131
+ # Checks if it's been update since last call of `#if_updated`.
132
+ def invalidate_if_updated
133
+ mtime = Styledown::FileReader.mtime(@paths)
134
+ if mtime != @mtime
135
+ invalidate
136
+ @mtime = mtime
137
+ end
138
+ end
139
+
140
+ # Applies data filters defined by `add_*_filter` functions.
141
+ def apply_data_filters(data)
142
+ @data_filters.reduce(data) do |data_, filter|
143
+ filter.(data_)
144
+ end
145
+ end
146
+ end
data/lib/styledown2.rb ADDED
@@ -0,0 +1 @@
1
+ require 'styledown'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ require File.expand_path('../lib/styledown/version', __FILE__)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'styledown2'
6
+ spec.version = Styledown::VERSION
7
+ spec.authors = ['Rico Sta. Cruz']
8
+ spec.email = ['rstacruz@users.noreply.github.com']
9
+
10
+ spec.summary = 'Write maintainable CSS styleguides using Markdown'
11
+ spec.description = 'Styledown lets you write maintainable CSS styleguides using Markdown.'
12
+ spec.homepage = 'https://github.com/styledown/styledown2'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'styledown2-source', "= #{Styledown::VERSION}"
22
+ spec.add_dependency 'execjs', '< 3.0.0'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'minitest'
25
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: styledown2
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0.pre5
5
+ platform: ruby
6
+ authors:
7
+ - Rico Sta. Cruz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: styledown2-source
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0.pre5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.0.pre5
27
+ - !ruby/object:Gem::Dependency
28
+ name: execjs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "<"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Styledown lets you write maintainable CSS styleguides using Markdown.
70
+ email:
71
+ - rstacruz@users.noreply.github.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - README.md
79
+ - Rakefile
80
+ - docs/api.md
81
+ - lib/styledown.rb
82
+ - lib/styledown/class_methods.rb
83
+ - lib/styledown/file_reader.rb
84
+ - lib/styledown/version.rb
85
+ - lib/styledown2.rb
86
+ - styledown2.gemspec
87
+ homepage: https://github.com/styledown/styledown2
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">"
103
+ - !ruby/object:Gem::Version
104
+ version: 1.3.1
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.5.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Write maintainable CSS styleguides using Markdown
111
+ test_files: []