livingstyleguide 0.6.0.alpha.2 → 1.0.0.alpha.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -2
  3. data/CHANGELOG.md +20 -0
  4. data/MIT-LICENSE.md +11 -0
  5. data/README.md +44 -6
  6. data/lib/livingstyleguide.rb +6 -27
  7. data/lib/livingstyleguide/code_block.rb +35 -0
  8. data/lib/livingstyleguide/engine.rb +93 -0
  9. data/lib/livingstyleguide/example.rb +86 -0
  10. data/lib/livingstyleguide/filter_hooks.rb +34 -0
  11. data/lib/livingstyleguide/filters.rb +8 -0
  12. data/lib/livingstyleguide/filters/add_wrapper_class.rb +4 -0
  13. data/lib/livingstyleguide/filters/coffee_script.rb +18 -0
  14. data/lib/livingstyleguide/filters/font_example.rb +20 -0
  15. data/lib/livingstyleguide/filters/full_width.rb +4 -0
  16. data/lib/livingstyleguide/filters/haml.rb +16 -0
  17. data/lib/livingstyleguide/filters/highlights.rb +9 -0
  18. data/lib/livingstyleguide/filters/javascript.rb +10 -0
  19. data/lib/livingstyleguide/importer.rb +19 -6
  20. data/lib/livingstyleguide/markdown_extensions.rb +24 -59
  21. data/lib/livingstyleguide/tilt_template.rb +30 -7
  22. data/lib/livingstyleguide/version.rb +1 -1
  23. data/livingstyleguide.gemspec +4 -1
  24. data/stylesheets/_livingstyleguide.scss +1 -1
  25. data/stylesheets/livingstyleguide/_content.scss +9 -8
  26. data/templates/layouts/default.html.erb +6 -6
  27. data/test/example_test_helper.rb +22 -0
  28. data/test/fixtures/markdown/code-block-and-example.md +8 -0
  29. data/test/fixtures/markdown/example-with-highlight.md +1 -1
  30. data/test/fixtures/markdown/example.md +1 -1
  31. data/test/fixtures/markdown/font-example.md +4 -1
  32. data/test/fixtures/markdown/{layout-example.md → full-width-example.md} +2 -1
  33. data/test/fixtures/markdown/haml-example-with-highlight.md +2 -1
  34. data/test/fixtures/markdown/haml-example.md +2 -1
  35. data/test/fixtures/standalone/modules/_buttons.md +1 -1
  36. data/test/integration/markdown_test.rb +17 -14
  37. data/test/integration/sprockets_test.rb +19 -0
  38. data/test/integration/standalone_test.rb +1 -1
  39. data/test/integration/variables_test.rb +1 -1
  40. data/test/test_helper.rb +9 -1
  41. data/test/unit/code_block_test.rb +53 -0
  42. data/test/unit/example_test.rb +142 -0
  43. data/test/unit/filter_hooks_test.rb +46 -0
  44. data/test/unit/filters/add_wrapper_class_test.rb +15 -0
  45. data/test/unit/filters/coffee_script_test.rb +19 -0
  46. data/test/unit/filters/font_example_test.rb +47 -0
  47. data/test/unit/filters/full_width_test.rb +16 -0
  48. data/test/unit/filters/haml_test.rb +20 -0
  49. data/test/unit/filters/highlights_test.rb +26 -0
  50. data/test/unit/filters/javascript_test.rb +18 -0
  51. data/test/unit/sass_extensions_test.rb +1 -1
  52. metadata +87 -10
  53. data/LICENSE.txt +0 -22
  54. data/lib/livingstyleguide/renderer.rb +0 -36
  55. data/test/fixtures/markdown/javascript-example.md +0 -4
@@ -0,0 +1,18 @@
1
+ LivingStyleGuide::Example.add_filter :coffee_script do
2
+ begin
3
+
4
+ require 'coffee-script'
5
+ @syntax = :'coffee-script'
6
+
7
+ add_wrapper_class '-lsg-for-javascript'
8
+
9
+ pre_processor do |coffee_script|
10
+ javascript = CoffeeScript.compile(coffee_script)
11
+ %Q(<script>#{javascript}</script>\n)
12
+ end
13
+
14
+ rescue LoadError
15
+ raise "Please make sure `gem 'coffee-script'` is added to your Gemfile."
16
+ end
17
+ end
18
+
@@ -0,0 +1,20 @@
1
+ LivingStyleGuide::Engine.default_options[:font_example] = { text: <<-TEXT }
2
+ ABCDEFGHIJKLMNOPQRSTUVWXYZ
3
+ abcdefghijklmnopqrstuvwxyz
4
+ 0123456789
5
+ !&/()$=@;:,.
6
+ TEXT
7
+
8
+ LivingStyleGuide::Example.add_filter :font_example do |font|
9
+ suppress_code_block
10
+
11
+ html do |content|
12
+ %(<div class="livingstyleguide--font-example" style="font: #{font}">\n#{content}\n</div>\n)
13
+ end
14
+
15
+ pre_processor do |content|
16
+ content = options[:font_example][:text] if content == ''
17
+ content.strip.gsub(/\n/, "<br>\n")
18
+ end
19
+ end
20
+
@@ -0,0 +1,4 @@
1
+ LivingStyleGuide::Example.add_filter :full_width do
2
+ add_wrapper_class '-lsg-has-full-width'
3
+ end
4
+
@@ -0,0 +1,16 @@
1
+ LivingStyleGuide::Example.add_filter :haml do
2
+ begin
3
+
4
+ require 'haml'
5
+ Haml::Options.defaults[:attr_wrapper] = '"'
6
+ @syntax = :haml
7
+
8
+ pre_processor do |haml|
9
+ Haml::Engine.new(haml).render.strip
10
+ end
11
+
12
+ rescue LoadError
13
+ raise "Please make sure `gem 'haml'` is added to your Gemfile."
14
+ end
15
+ end
16
+
@@ -0,0 +1,9 @@
1
+ LivingStyleGuide::CodeBlock.filter_code do |code|
2
+ code = code.gsub(/^\s*\*\*\*\n(.+?)\n\s*\*\*\*(\n|$)/m, %Q(\n<strong class="livingstyleguide--code-highlight-block">\\1</strong>\n))
3
+ code = code.gsub(/\*\*\*(.+?)\*\*\*/, %Q(<strong class="livingstyleguide--code-highlight">\\1</strong>))
4
+ end
5
+
6
+ LivingStyleGuide::Example.filter_before do |code|
7
+ code.gsub(/\*\*\*(.+?)\*\*\*/m, '\\1')
8
+ end
9
+
@@ -0,0 +1,10 @@
1
+ LivingStyleGuide::Example.add_filter :javascript do
2
+ @syntax = :javascript
3
+
4
+ add_wrapper_class '-lsg-for-javascript'
5
+
6
+ pre_processor do |javascript|
7
+ %Q(<script>#{javascript}</script>\n)
8
+ end
9
+ end
10
+
@@ -5,23 +5,36 @@ class LivingStyleGuide::Importer < Sass::Importers::Filesystem
5
5
  end
6
6
 
7
7
  def find_relative(name, base, options, absolute = false)
8
- find_markdown(File.join(File.dirname(base), name))
9
- super(name, base, options)
8
+ @options = options
9
+ engine = super(name, base, options)
10
+ find_markdown(options[:filename])
11
+ engine
10
12
  end
11
13
 
12
14
  def find(name, options)
13
- find_markdown(name)
15
+ @options = options
14
16
  super(name, options)
15
17
  end
16
18
 
19
+ private
17
20
  def find_markdown(sass_filename)
21
+ files << sass_filename
18
22
  glob = "#{sass_filename.sub(/\.s[ac]ss$/, '')}.md"
19
- glob.sub!(/(.*)\//, '\\1/{_,}') unless glob =~ /\/_/
20
- glob = '{_,}' + glob unless glob =~ /\//
21
23
  Dir.glob(glob) do |markdown_filename|
22
- LivingStyleGuide.add_markdown File.read(markdown_filename)
24
+ files << markdown_filename
25
+ markdown << File.read(markdown_filename)
23
26
  end
24
27
  end
25
28
 
29
+ private
30
+ def files
31
+ @options[:living_style_guide].files
32
+ end
33
+
34
+ private
35
+ def markdown
36
+ @options[:living_style_guide].markdown
37
+ end
38
+
26
39
  end
27
40
 
@@ -2,39 +2,26 @@ require 'redcarpet'
2
2
  require 'tilt'
3
3
  require 'minisyntax'
4
4
  require 'erb'
5
- require 'active_support/core_ext/string/inflections'
6
5
 
7
6
  module LivingStyleGuide
8
- class RedcarpetTemplate < ::Tilt::RedcarpetTemplate::Redcarpet2
9
- RENDER_OPTIONS = {
10
- :autolink => true,
11
- :fenced_code_blocks => true,
12
- :tables => true,
13
- :strikethrough => true,
14
- :space_after_headers => true,
15
- :superscript => true
16
- }
7
+ REDCARPET_RENDER_OPTIONS = {
8
+ autolink: true,
9
+ fenced_code_blocks: true,
10
+ tables: true,
11
+ strikethrough: true,
12
+ space_after_headers: true,
13
+ superscript: true
14
+ }
17
15
 
18
- def generate_renderer
19
- RedcarpetHTML.new(RENDER_OPTIONS)
20
- end
21
-
22
- def prepare
23
- @engine = ::Redcarpet::Markdown.new(generate_renderer, RENDER_OPTIONS)
24
- @output = nil
25
- end
16
+ class RedcarpetHTML < ::Redcarpet::Render::HTML
26
17
 
27
- def evaluate(context, locals, &block)
28
- @context ||= context
18
+ def initialize(options = {})
19
+ @options = options
29
20
  super
30
21
  end
31
22
 
32
- end
33
-
34
- class RedcarpetHTML < ::Redcarpet::Render::HTML
35
-
36
23
  def header(text, header_level)
37
- id = %Q( id="#{::ActiveSupport::Inflector.parameterize(text, '-')}")
24
+ id = %Q( id="#{slug(text)}")
38
25
  klass = %w(page-title headline sub-headline sub-sub-headline)[header_level]
39
26
  header_level += 1
40
27
  %Q(<h#{header_level} class="livingstyleguide--#{klass}"#{id}>#{text}</h#{header_level}>\n)
@@ -48,16 +35,6 @@ module LivingStyleGuide
48
35
  result << %Q(<li class="livingstyleguide--color-swatch $#{variable}">$#{variable}</li>\n)
49
36
  end
50
37
  result << "</ul>\n"
51
- elsif text =~ /^\{\{font-example:(.+)\}\}$/
52
- font = $1
53
- <<-HTML.gsub(' ', '')
54
- <div class="livingstyleguide--font-example" style="font: #{font}">
55
- ABCDEFGHIJKLMNOPQRSTUVWXYZ<br>
56
- abcdefghijklmnopqrstuvwxyz<br>
57
- 0123456789<br>
58
- !&/()$=@;:,.
59
- </div>
60
- HTML
61
38
  else
62
39
  %Q(<p class="livingstyleguide--paragraph">#{text}</p>\n)
63
40
  end
@@ -73,29 +50,11 @@ module LivingStyleGuide
73
50
  end
74
51
 
75
52
  def block_code(code, language)
76
- if %w(example layout-example).include?(language)
77
- html = code.gsub(/\*\*\*(.+?)\*\*\*/m, '\\1')
78
- %Q(<div class="livingstyleguide--#{language}">\n #{html}\n</div>) + "\n" + block_code(code, 'html')
79
- elsif %w(haml-example haml-layout-example).include?(language)
80
- begin
81
- type = language[5..-1]
82
- require 'haml'
83
- Haml::Options.defaults[:attr_wrapper] = '"'
84
- haml = code.gsub(/\*\*\*(.+?)\*\*\*/m, '\\1')
85
- html = Haml::Engine.new(haml).render.strip
86
- %Q(<div class="livingstyleguide--#{type}">\n #{html}\n</div>) + "\n" + block_code(code, 'haml')
87
- rescue LoadError
88
- raise "Please make sure `gem 'haml'` is added to your Gemfile."
89
- end
90
- elsif %w(javascript-example).include?(language)
91
- javascript = code.gsub(/\*\*\*(.+?)\*\*\*/m, '\\1')
92
- %Q(<script>#{javascript}</script>\n) + block_code(code, 'javascript')
53
+ language ||= @options[:default_language]
54
+ if language == 'example'
55
+ Example.new(code, @options).render
93
56
  else
94
- code = ERB::Util.html_escape(code).gsub(/&quot;/, '"')
95
- code = ::MiniSyntax.highlight(code.strip, language.to_s.strip.to_sym)
96
- code.gsub! /^\s*\*\*\*\n(.+?)\n\s*\*\*\*(\n|$)/m, %Q(<strong class="livingstyleguide--code-highlight-block">\\1</strong>)
97
- code.gsub! /\*\*\*(.+?)\*\*\*/, %Q(<strong class="livingstyleguide--code-highlight">\\1</strong>)
98
- %Q(<pre class="livingstyleguide--code-block"><code class="livingstyleguide--code">#{code}</code></pre>)
57
+ CodeBlock.new(code.strip, language.to_s.strip.to_sym).render
99
58
  end
100
59
  end
101
60
 
@@ -104,8 +63,14 @@ module LivingStyleGuide
104
63
  %Q(<code class="livingstyleguide--code-span livingstyleguide--code">#{code}</code>)
105
64
  end
106
65
 
107
- end
66
+ private
67
+ def slug(text)
68
+ require 'active_support/core_ext/string/inflections'
69
+ ::ActiveSupport::Inflector.parameterize(text, '-')
70
+ rescue LoadError
71
+ text.downcase.gsub(/[ _\.\-!\?\(\)\[\]]+/, '-').gsub(/^-|-$/, '')
72
+ end
108
73
 
109
- ::Tilt.register RedcarpetTemplate, 'markdown', 'mkd', 'md'
74
+ end
110
75
  end
111
76
 
@@ -11,6 +11,7 @@ module ::Tilt
11
11
  end
12
12
 
13
13
  def evaluate(scope, locals, &block)
14
+ @scope = scope
14
15
  parse_options(data)
15
16
  generate_sass
16
17
  render_living_style_guide
@@ -25,11 +26,11 @@ module ::Tilt
25
26
  options[:template_location].each do |path, short|
26
27
  options[:load_paths] << ::LivingStyleGuide::Importer.new(path)
27
28
  end
28
- options[:filename] = eval_file
29
- options[:line] = line
30
- options[:syntax] = @options[:syntax]
31
- options[:importer] = LivingStyleGuide::Importer.new('.')
32
- options[:living_style_guide] = @options
29
+ options[:filename] = eval_file
30
+ options[:line] = line
31
+ options[:syntax] = @options[:syntax]
32
+ options[:importer] = LivingStyleGuide::Importer.new('.')
33
+ options[:sprockets] = { context: @scope }
33
34
  options
34
35
  end
35
36
 
@@ -52,6 +53,26 @@ module ::Tilt
52
53
  ].flatten.join(@options[:syntax] == :sass ? "\n" : ';')
53
54
  end
54
55
 
56
+ private
57
+ def configure_cache
58
+ return unless @scope.respond_to?(:depend_on)
59
+ test = /^#{root}/
60
+ @engine.files.uniq.each do |file|
61
+ if file =~ test
62
+ @scope.depend_on file
63
+ end
64
+ end
65
+ end
66
+
67
+ private
68
+ def root
69
+ if @scope.respond_to?(:environment)
70
+ @scope.environment.root
71
+ else
72
+ File.dirname(@file)
73
+ end
74
+ end
75
+
55
76
  private
56
77
  def style_variables
57
78
  return unless @options.has_key?(:style)
@@ -62,8 +83,10 @@ module ::Tilt
62
83
 
63
84
  private
64
85
  def render_living_style_guide
65
- engine = ::Sass::Engine.new(@sass, sass_options)
66
- engine.render_living_style_guide
86
+ @engine = ::LivingStyleGuide::Engine.new(@sass, @options, sass_options)
87
+ html = @engine.render
88
+ configure_cache
89
+ html
67
90
  end
68
91
  end
69
92
 
@@ -1,3 +1,3 @@
1
1
  module LivingStyleGuide
2
- VERSION = "0.6.0.alpha.2"
2
+ VERSION = "1.0.0.alpha.3"
3
3
  end
@@ -22,9 +22,12 @@ Gem::Specification.new do |gem|
22
22
  gem.add_dependency 'sass'
23
23
  gem.add_dependency 'redcarpet'
24
24
  gem.add_dependency 'tilt'
25
- gem.add_dependency 'activesupport'
26
25
  gem.add_dependency 'thor'
26
+ gem.add_dependency 'hooks'
27
27
 
28
28
  gem.add_development_dependency 'rake'
29
29
  gem.add_development_dependency 'haml'
30
+ gem.add_development_dependency 'heredoc_unindent'
31
+ gem.add_development_dependency 'minitest'
32
+ gem.add_development_dependency 'coffee-script'
30
33
  end
@@ -29,7 +29,7 @@ $livingstyleguide--border-radius: 0 !default;
29
29
  $livingstyleguide--color: #505050 !default;
30
30
  $livingstyleguide--width: 640px !default;
31
31
  $livingstyleguide--gap-width: 10px !default;
32
- $livingstyleguide--layout-example-padding: $livingstyleguide--gap-width !default;
32
+ $livingstyleguide--full-width-padding: $livingstyleguide--gap-width !default;
33
33
 
34
34
  $livingstyleguide--code-background-color: #505050 !default;
35
35
  $livingstyleguide--code-color: white !default;
@@ -62,17 +62,18 @@
62
62
  @include box-sizing(border-box);
63
63
  @include border-top-radius($livingstyleguide--border-radius);
64
64
 
65
- .livingstyleguide--layout-example & {
65
+ &.-lsg-has-full-width {
66
+ @extend #{$livingstyleguide--layout-selector} !optional;
67
+ display: block;
68
+ height: auto;
66
69
  margin: 0 auto;
70
+ min-height: auto;
71
+ padding: $livingstyleguide--full-width-padding;
67
72
  width: 100%;
68
73
  @include border-radius(0);
69
74
  }
70
- }
71
75
 
72
- .livingstyleguide--layout-example {
73
- @extend #{$livingstyleguide--layout-selector} !optional;
74
- display: block;
75
- height: auto;
76
- min-height: auto;
77
- padding: $livingstyleguide--layout-example-padding;
76
+ &.-lsg-for-javascript {
77
+ display: none;
78
+ }
78
79
  }
@@ -5,21 +5,21 @@
5
5
  <meta charset="utf-8">
6
6
  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
7
7
  <meta content="The LivingStyleGuide Gem – http://livingstyleguide.org" name="generator">
8
- <title><%= h(@title) %></title>
8
+ <title><%= h(title) %></title>
9
9
  <script>
10
10
  // see: http://www.hagenburger.net/BLOG/Simple-HTML5-Fix-for-IE.html
11
11
  for(var e,l='article aside footer header nav section time picture'.split(' ');e=l.pop();document.createElement(e));
12
12
  </script>
13
13
  <style>
14
- <%= @css %>
14
+ <%= css %>
15
15
  </style>
16
- <%= @head %>
16
+ <%= head %>
17
17
  </head>
18
18
 
19
19
  <body class="livingstyleguide">
20
- <%= @header %>
21
- <%= @html %>
22
- <%= @footer %>
20
+ <%= header %>
21
+ <%= html %>
22
+ <%= footer %>
23
23
  </body>
24
24
 
25
25
  </html>
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class ExampleTestCase < Minitest::Test
4
+
5
+ def setup
6
+ @class = Class.new(LivingStyleGuide::Example)
7
+ end
8
+
9
+ def assert_render_equals(input, expected_output, options = nil)
10
+ options ||= LivingStyleGuide::Engine.default_options
11
+ output = @class.new(input.unindent, options).render
12
+ assert_equal(normalize(expected_output), normalize(output))
13
+ end
14
+
15
+ def assert_render_match(input, expected_output, options = nil)
16
+ options ||= LivingStyleGuide::Engine.default_options
17
+ output = @class.new(input.unindent, options).render
18
+ assert_match(/#{normalize(expected_output)}/, normalize(output))
19
+ end
20
+
21
+ end
22
+
@@ -0,0 +1,8 @@
1
+ ~~~
2
+ Normal code block
3
+ ~~~
4
+
5
+ ~~~ example
6
+ Example
7
+ ~~~
8
+
@@ -1,4 +1,4 @@
1
- ~~~ example
1
+ ~~~
2
2
  <img class="inline ***example***">
3
3
  <img class="inline ***ex-1*** ***ex-2***">
4
4
  ~~~
@@ -1,4 +1,4 @@
1
- ~~~ example
1
+ ~~~
2
2
  <button class="button">Test</button>
3
3
  ~~~
4
4