writedown 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff8da87d42e5c852fc414144e24aa900ba2ad103
4
+ data.tar.gz: bfdcd2f44d8d44342dfb5dab9b7e2ef8bc9dc9ed
5
+ SHA512:
6
+ metadata.gz: d5e6839151cf4b8ef4350df81db371fbabde01ee8c911443af5916ada6c7a9c1f3fcaa85f5c563352d14123fead09b355f8834b1d3eb8fa191b73698d2898530
7
+ data.tar.gz: '0582b54d6f3d5501882687e657a22a9c29fda420ff0fd2533285a98d5ca675503855f2aff992da5cf7b83bf07b18e4ff8daf063e18a125773c644ad5af2f9397'
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
@@ -0,0 +1,10 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=4a802850f20f8a5a315c700bd20153cc452ddb11b299f67ab994fc0199278d86
4
+ language: ruby
5
+ before_script:
6
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
7
+ - chmod +x ./cc-test-reporter
8
+ - ./cc-test-reporter before-build
9
+ after_script:
10
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'byebug', '~> 9.0'
7
+ gem 'rake', '~> 12.0'
8
+ gem 'minitest', '~> 5.10'
9
+ gem 'simplecov', '~> 0.15.0'
10
+ gem 'sinatra', '~> 2.0'
11
+ gem 'thin', '~> 1.7'
12
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Adam Hollett
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.
@@ -0,0 +1,130 @@
1
+ # Writedown
2
+
3
+ [![Travis](https://img.shields.io/travis/adamhollett/writedown.svg)](https://travis-ci.org/adamhollett/writedown)
4
+ [![Code Climate](https://img.shields.io/codeclimate/maintainability/adamhollett/writedown.svg)](https://codeclimate.com/github/adamhollett/writedown)
5
+ [![Code Climate](https://img.shields.io/codeclimate/c/adamhollett/writedown.svg)](https://codeclimate.com/github/adamhollett/writedown)
6
+
7
+ **Writedown** is an enhanced renderer for [kramdown](https://github.com/gettalong/kramdown), the pure Ruby markdown converter.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'writedown'
15
+ ```
16
+
17
+ Then execute:
18
+
19
+ ``` shell
20
+ bundle
21
+ ```
22
+
23
+ or install it yourself with:
24
+
25
+ ``` shell
26
+ gem install writedown
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ After installing the gem, replace instances of `Kramdown::Document#to_html` in your code with `Kramdown::Document#to_writedown`.
32
+
33
+ ## Features
34
+
35
+ ### Asides
36
+
37
+ You can render asides or "margin notes" using an extended blockquote syntax:
38
+
39
+ Markdown
40
+
41
+ ``` markdown
42
+ > Note:
43
+ > Don't forget to turn off the power.
44
+ ```
45
+
46
+ HTML
47
+
48
+ ``` html
49
+ <aside class="aside aside--note">
50
+ <h4>Note</h4>
51
+ <p>Don't forget to turn off the power.</p>
52
+ </aside>
53
+ ```
54
+
55
+ ### Checkboxes
56
+
57
+ You can render checkboxes in paragraphs or list items by adding a box to the beginning of the item. Fill the box with an `x` to have it pre-checked when the page is rendered:
58
+
59
+ Markdown
60
+
61
+ ``` markdown
62
+ - [ ] Buy dog food
63
+ - [ ] Pick up laundry
64
+ - [x] Pay bills
65
+ ```
66
+
67
+ HTML
68
+
69
+ ``` html
70
+ <ul>
71
+ <li>
72
+ <p>
73
+ <input type="checkbox" id="checkbox-1" />
74
+ <label for "checkbox-1">Buy dog food</label>
75
+ </p>
76
+ </li>
77
+ <li>
78
+ <p>
79
+ <input type="checkbox" id="checkbox-2" />
80
+ <label for "checkbox-2">Pick up laundry</label>
81
+ </p>
82
+ </li><li>
83
+ <p>
84
+ <input type="checkbox" id="checkbox-3" checked="checked" />
85
+ <label for "checkbox-3">Pay bills</label>
86
+ </p>
87
+ </li>
88
+ </ul>
89
+ ```
90
+
91
+ ### Figures
92
+
93
+ You can convert an image into an HTML5 figure by placing it on its own line without any other text. Passing a title to the image renders the title as a figure caption:
94
+
95
+ Markdown
96
+
97
+ ``` markdown
98
+ ![A still life with fruits and flowers](picture.jpg "Still life")
99
+ ```
100
+
101
+ HTML
102
+
103
+ ``` html
104
+ <figure>
105
+ <img src="picture.jpg" alt="A still life with fruits and flowers" title="Still life" />
106
+ <figcaption>Still life</figcaption>
107
+ </figure>
108
+ ```
109
+
110
+ ### Image dimensions
111
+
112
+ If you embed a local image, Writedown uses [fastimage](https://github.com/sdsykes/fastimage) to check the image's dimensions and pass them to the HTML:
113
+
114
+ ``` markdown
115
+ ![](picture.jpg)
116
+ ```
117
+
118
+ HTML
119
+
120
+ ``` html
121
+ <img src="picture.jpg" alt="" width="640" height="480" />
122
+ ```
123
+
124
+ ## Contributing
125
+
126
+ Bug reports and pull requests are welcome.
127
+
128
+ ## License
129
+
130
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ task default: :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << 'test'
10
+ t.test_files = FileList['test/**/*_test.rb']
11
+ t.warning = false
12
+ end
13
+ task t: :test
14
+
15
+ desc 'Start a console session'
16
+ task :console do
17
+ sh 'ruby bin/console'
18
+ end
19
+ task c: :console
20
+
21
+ desc 'Start a server to test Writedown in a browser'
22
+ task :server do
23
+ sh 'ruby test/app/app.rb'
24
+ end
25
+ task s: :server
26
+
27
+ desc 'Check dependencies'
28
+ task :bundle do
29
+ sh 'bundle --quiet'
30
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'kramdown'
6
+ require 'writedown'
7
+ require 'irb'
8
+
9
+ IRB.start(__FILE__)
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ # TODO: - [x] Asides (note blocks)
4
+ # - [x] Checkboxes
5
+ # Give checkboxes IDs from the line number in the source document
6
+ # - [x] Checkboxes pre-filled with Xs
7
+ # - [x] Figures
8
+ # - [ ] Video embeds
9
+
10
+ module Kramdown
11
+ module Converter
12
+ class Writedown < Html
13
+ include ::Writedown::Image
14
+ include ::Writedown::Aside
15
+ include ::Writedown::Checkbox
16
+ include ::Writedown::Figure
17
+
18
+ def convert_blockquote(el, indent)
19
+ if el.children.first.children.first.value =~ ASIDE_PATTERN
20
+ convert_aside(el, indent)
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def convert_p(el, indent)
27
+ if el.children.size == 1 && el.children.first.type == :img
28
+ convert_figure(el, indent)
29
+ elsif el.children.first.value =~ CHECKBOX_PATTERN
30
+ convert_checkbox(el, indent)
31
+ else
32
+ super
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kramdown'
4
+ require 'writedown/utils'
5
+ require 'writedown/image'
6
+ require 'writedown/aside'
7
+ require 'writedown/checkbox'
8
+ require 'writedown/figure'
9
+ require 'writedown/version'
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Writedown
4
+ module Aside
5
+ ASIDE_HEADINGS = %w(
6
+ note
7
+ info information tip
8
+ caution warning danger
9
+ )
10
+
11
+ ASIDE_PATTERN = /(#{ASIDE_HEADINGS.join('|')}):?\n/i
12
+
13
+ def convert_aside(el, indent)
14
+ el.type = :aside
15
+ text_content = el.children.first.children.first.value
16
+
17
+ # Determine the heading string used
18
+ heading_text = text_content.match(ASIDE_PATTERN)[1]
19
+
20
+ base_class = 'aside'
21
+ aside_classes = [base_class]
22
+ heading_slug = Writedown::Utils.slugify(heading_text)
23
+ aside_classes << "aside--#{heading_slug}" unless heading_slug == base_class
24
+
25
+ el.attr[:class] = aside_classes.join(' ')
26
+
27
+ # Remove the note pattern from the body contents
28
+ text_content.sub!(ASIDE_PATTERN, '')
29
+
30
+ heading_element = Kramdown::Element.new(:header, nil, nil, level: 4, raw_text: heading_text)
31
+ heading_element.children << Kramdown::Element.new(:text, heading_text.capitalize)
32
+
33
+ el.children.unshift(heading_element)
34
+
35
+ format_as_indented_block_html(el.type, el.attr, inner(el, indent), indent)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Writedown
4
+ module Checkbox
5
+ CHECKBOX_PATTERN = /^\[([ xX]?)\] ?/
6
+
7
+ def convert_checkbox(el, indent)
8
+ text_content = el.children.first.value
9
+
10
+ el.options[:transparent] = false
11
+ box_contents = text_content.match(CHECKBOX_PATTERN)[1]
12
+ text_content = text_content.sub(CHECKBOX_PATTERN, '')
13
+ checked = 'checked' if box_contents =~ /[xX]/
14
+ id = "checkbox-#{el.options[:location]}"
15
+
16
+ el.children = []
17
+
18
+ # Create the input element
19
+ input_attrs = { type: 'checkbox', id: id, checked: checked }
20
+ input_element = Kramdown::Element.new(
21
+ :html_element, :input, input_attrs, category: :block, content_model: :raw, is_closed: true
22
+ )
23
+
24
+ # Create the label
25
+ label_element = Kramdown::Element.new(:html_element, :label, { for: id }, category: :span, content_model: :span)
26
+ label_element.children << Kramdown::Element.new(:text, text_content)
27
+
28
+ el.children << input_element
29
+ el.children << label_element
30
+
31
+ format_as_indented_block_html(el.type, el.attr, inner(el, indent), indent)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Writedown
4
+ module Figure
5
+ def convert_figure(el, indent)
6
+ el.type = :figure
7
+
8
+ caption = el.children.first.attr['title']
9
+ if caption
10
+ caption_text = Kramdown::Element.new(:text, caption)
11
+ caption_element = Kramdown::Element.new(:html_element, :figcaption, nil, category: :block)
12
+ caption_element.children << caption_text
13
+ el.children << caption_element
14
+ end
15
+
16
+ format_as_indented_block_html(el.type, el.attr, inner(el, indent), indent)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fastimage'
4
+
5
+ module Writedown
6
+ module Image
7
+ def convert_img(el, indent)
8
+ path = el.attr['src']
9
+ unless Writedown::Utils.remote?(path)
10
+ image_path = Dir.glob("**#{path}").first
11
+ dimensions = FastImage.size(image_path)
12
+
13
+ if dimensions
14
+ el.attr[:width] = dimensions.first
15
+ el.attr[:height] = dimensions.last
16
+ end
17
+ end
18
+
19
+ super
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Writedown
4
+ class Utils
5
+ class << self
6
+ def remote?(path)
7
+ !!path.match(%r{(?:https?|//)})
8
+ end
9
+
10
+ def slugify(string, sep = '-')
11
+ slugified_string = string
12
+ slugified_string.gsub!(/[^\w\-_]+/, sep)
13
+ unless sep.nil? || sep.empty?
14
+ re_sep = Regexp.escape(sep)
15
+ # No more than one of the separator in a row
16
+ slugified_string.gsub!(/#{re_sep}{2,}/, sep)
17
+ # Remove leading/trailing separator
18
+ slugified_string.gsub!(/^#{re_sep}|#{re_sep}$/, '')
19
+ end
20
+ slugified_string.downcase
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Writedown
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'writedown/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'writedown'
8
+ s.version = Writedown::VERSION
9
+ s.authors = ['Adam Hollett']
10
+ s.email = ['mail@adamhollett.com']
11
+
12
+ s.summary = 'An enhanced renderer for kramdown, the pure Ruby Markdown converter.'
13
+ s.homepage = 'https://github.com/admhlt/writedown'
14
+ s.license = 'MIT'
15
+
16
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ s.require_paths = ['lib']
20
+
21
+ s.add_dependency 'kramdown', '~> 1.16', '>= 1.16.0'
22
+ s.add_dependency 'fastimage', '~> 2.0', '>= 2.0.0'
23
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: writedown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Hollett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kramdown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.16.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.16'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.16.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: fastimage
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.0.0
53
+ description:
54
+ email:
55
+ - mail@adamhollett.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - ".gitignore"
61
+ - ".travis.yml"
62
+ - Gemfile
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - bin/console
67
+ - lib/kramdown/converter/writedown.rb
68
+ - lib/writedown.rb
69
+ - lib/writedown/aside.rb
70
+ - lib/writedown/checkbox.rb
71
+ - lib/writedown/figure.rb
72
+ - lib/writedown/image.rb
73
+ - lib/writedown/utils.rb
74
+ - lib/writedown/version.rb
75
+ - writedown.gemspec
76
+ homepage: https://github.com/admhlt/writedown
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.14
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: An enhanced renderer for kramdown, the pure Ruby Markdown converter.
100
+ test_files: []