Skip to content

Add html options #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/tmp/
.rbenv-gemsets
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in markdown_checkboxes.gemspec
gemspec
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ Assuming you have your infrastructure set up accordingly, this should send an HT
your post's body, as well as fire unobtrusive javascript after the action is completed (allowing you to do
things like prevent a page refresh, and other cool js things)

### Passing html options

Also, it is possible to pass html options to rendered checkboxes.

You can pass second argument to `#render` method with hash of options:

```ruby
markdown = '- [ ] - [x]'
html_options = { disabled: true }

parser.render(markdown, html_options) do |data, updated_text|
# ...
end
```

Result will look like follows:

```html
<input type="checkbox" name="check_1" id="check_1" value="" disabled="disabled">
<input type="checkbox" name="check_2" id="check_2" value="" disabled="disabled" checked="checked">
```

### Installation

```
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ Rake::TestTask.new do |t|
t.libs << 'test'
end

desc "Run tests"
desc 'Run tests'
task :default => :test
44 changes: 18 additions & 26 deletions lib/markdown_checkboxes.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,37 @@
require 'redcarpet'
require 'action_view'
require File.dirname(__FILE__) + '/markdown_checkboxes/data_struct'
require 'markdown_checkboxes/data_struct'

class CheckboxMarkdown < Redcarpet::Markdown
include ActionView::Helpers::FormTagHelper

VERSION = '1.0.0'
CHECKBOX_REGEX = /-\s?\[(x|\s)\]/

def render(text, &block)
text = parse_with_checkboxes(text, &block)
super(text)
def render(text, html_options = {}, &block)
super(parse_with_checkboxes(text, html_options, &block))
end

private

def parse_with_checkboxes(text, &block)
checkbox_regex = /-\s?\[(x|\s)\]/
def parse_with_checkboxes(text, html_options, &block)
raw_text = html_options.delete(:raw_text) { text }

text.gsub(checkbox_regex).with_index do |current_match, current_index|
checked = current_match =~ /x/ ? true : false
text.gsub(CHECKBOX_REGEX).with_index do |current_match, current_index|
checked = current_match =~ /x/
body = updated_body(raw_text, current_index, checked)

body =
text.gsub(checkbox_regex).with_index do |match, index|
if index == current_index
checked ? "- [ ]" : "- [x]"
else
match
end
end

check_box_tag "check_#{current_index}", "", checked, data: data_options(body, &block)
check_box_tag("check_#{current_index}", '', checked, **html_options, data: data_options(body, &block))
end
end

def data_options(body)
if block_given?
data_struct = DataStruct.new
yield(data_struct, body)
data_struct.serializable_hash
else
{}
def data_options(body, &block)
DataStruct.new(body, &block).data
end

def updated_body(text, current_index, current_checked)
text.gsub(CHECKBOX_REGEX).with_index do |match, index|
next match if index != current_index
current_checked ? '- [ ]' : '- [x]'
end
end

Expand Down
27 changes: 17 additions & 10 deletions lib/markdown_checkboxes/data_struct.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
class DataStruct
attr_accessor :data

def initialize
def initialize(body)
@data = {}
yield(self, body) if block_given?
end

def serializable_hash
Hash[@data.map { |k,v| [k.to_s, v.to_s] }]
def method_missing(name, *args, &block)
return super if !is_setter(name)
@data[dashed(name)] = args.first.to_s
end

def method_missing(name, *args, &block)
if name.to_s =~ /=$/
@data[name.to_s.gsub('_', '-').chop] = args.first
else
super(name, *args, &block)
end
def respond_to_missing?(method_name, include_private = false)
is_setter(method_name) || @data.has_key?(dashed(method_name)) || super
end
end

private

def is_setter(method_name)
!!(method_name.to_s =~ /=\z/)
end

def dashed(value)
value.to_s.gsub('_', '-').sub(/=\z/, '')
end
end
Binary file removed markdown_checkboxes-1.0.0.gem
Binary file not shown.
Binary file added markdown_checkboxes-1.1.0.gem
Binary file not shown.
34 changes: 26 additions & 8 deletions markdown_checkboxes.gemspec
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
require File.dirname(__FILE__) + '/lib/markdown_checkboxes'
# coding: utf-8

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |s|
s.name = 'markdown_checkboxes'
s.version = CheckboxMarkdown::VERSION
s.date = '2013-11-20'
s.version = '1.1.0'
s.date = '2017-03-28'
s.summary = 'Markdown with checkbox support'
s.description = 'Adding checkbox rendering functionality on top of the redcarpet markdown parser'
s.authors = ['Brightbit Apps']
s.email = '[email protected]'
s.files = ['lib/markdown_checkboxes.rb', 'lib/markdown_checkboxes/data_struct.rb']
s.homepage = 'http://rubygems.org/gems/markdown_checkboxes'
s.license = 'MIT'

s.add_runtime_dependency "redcarpet", '>= 3.0.0'
s.add_runtime_dependency "actionpack", '>= 2.0.0'
if s.respond_to?(:metadata)
s.metadata['allowed_push_host'] = 'https://rubygems.org'
else
raise 'RubyGems 2.0 or newer is required to protect against ' \
'public gem pushes.'
end

s.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
f.match(/\.gem\z/)
end

s.bindir = 'exe'
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
s.require_paths = %w(lib)

s.add_runtime_dependency 'redcarpet', '~> 3.4'
s.add_runtime_dependency 'actionpack', '~> 5.0'

s.add_development_dependency "rake-compiler", "~> 0.8.3"
s.add_development_dependency "test-unit", "~> 2.5.4"
s.add_development_dependency 'rake-compiler', '~> 1.0'
s.add_development_dependency 'test-unit', '~> 3.2'
end
34 changes: 20 additions & 14 deletions test/test_data_struct.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/markdown_checkboxes/data_struct'
require 'markdown_checkboxes/data_struct'

class DataStructTest < Test::Unit::TestCase

def setup
@struct = DataStruct.new
@struct.key = "value"
@struct = DataStruct.new('Lorem ipsum...')
@struct.key = 'value'
@struct.test = true
end

def test_data_struct_exists
assert @struct.is_a? DataStruct
end

def test_methods_turn_into_key_value_pairs
assert_equal @struct.data["key"], "value"
assert_equal @struct.data["test"], true
end

def test_serializable_hash
assert_equal @struct.serializable_hash, { "key" => "value", "test" => "true" }
def test_methods_turn_into_stringified_key_value_pairs
assert_equal @struct.data['key'], 'value'
assert_equal @struct.data['test'], 'true'
end

def test_underscores_turning_to_dashes
@struct.okc_thunder = "awesome"
assert @struct.data.has_key? "okc-thunder"
assert_equal @struct.data["okc-thunder"], 'awesome'
@struct.okc_thunder = 'awesome'
assert @struct.data.has_key?('okc-thunder')
assert_equal @struct.data['okc-thunder'], 'awesome'
end

def test_still_calls_real_method_missing
Expand All @@ -34,5 +30,15 @@ def test_still_calls_real_method_missing
end
end

end
def test_responds_to_setters
assert_respond_to @struct, :setter=
assert_respond_to @struct, :alt_setter=
end

def test_responds_to_data_keys_getters
@struct.okc_thunder = 'awesome'
assert_respond_to @struct, :okc_thunder
refute_respond_to @struct, :getter
end

end
42 changes: 28 additions & 14 deletions test/test_markdown_checkboxes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/markdown_checkboxes'
require 'markdown_checkboxes'

class MarkdownCheckboxesTest < Test::Unit::TestCase

def setup
@m ||= CheckboxMarkdown.new(Redcarpet::Render::HTML.new())
@m ||= CheckboxMarkdown.new(Redcarpet::Render::HTML.new)
end

def test_proper_markdown_inheritance
Expand All @@ -13,32 +13,46 @@ def test_proper_markdown_inheritance
end

def test_standard_markdown
assert_equal @m.render("## Hello"), "<h2>Hello</h2>\n"
assert_equal @m.render("**Bold**"), "<p><strong>Bold</strong></p>\n"
assert_equal @m.render('## Hello'), "<h2>Hello</h2>\n"
assert_equal @m.render('**Bold**'), "<p><strong>Bold</strong></p>\n"
end

def test_checkbox_existence
assert_match /<input.* \/>/, @m.render("- [ ]")
assert_match /type="checkbox"/, @m.render("- [ ]")
assert_match(/<input.* \/>/, @m.render('- [ ]'))
assert_match(/type="checkbox"/, @m.render('- [ ]'))
end

def test_checkbox_check_attribute
assert_match /checked="checked"/, @m.render("- [x]")
assert_no_match /checked="checked"/, @m.render("- [ ]")
assert_match(/checked="checked"/, @m.render('- [x]'))
assert_no_match(/checked="checked"/, @m.render('- [ ]'))
end

def test_checkbox_data_setting
assert_match(/data-remote="true"/,
@m.render("- [ ]") do |data, updated_text|
data.remote = true
end
assert_match(
/data-remote="true"/,
@m.render('- [ ]') { |data, _| data.remote = true }
)

assert_match(/data-method="put"/,
@m.render("- [x]") do |data, updated_text|
assert_match(
/data-method="put"/,
@m.render('- [x]') do |data, _|
data.remote = true
data.method = :put
end
)
end

def test_checkbox_disabled_html_option
assert_match(/disabled="disabled"/, @m.render('- [ ]', disabled: true))
assert_match(/disabled="disabled"/, @m.render('- [x]', disabled: true))
refute_match(/disabled/, @m.render('- [ ]', disabled: false))
refute_match(/disabled/, @m.render('- [x]', disabled: false))
refute_match(/disabled/, @m.render('- [ ]'))
refute_match(/disabled/, @m.render('- [x]'))
end

def test_raw_text_in_html_options
assert_match(/lorem\sipsum/, @m.render('- [ ]', disabled: true, raw_text: 'lorem ipsum') { |data, text| data.url = 'path?key=' + text })
assert_match(/hello\sworld/, @m.render('- [ ]', disabled: false, raw_text: 'hello world') { |data, text| data.url = 'path?key=' + text })
end
end