From e6cb4c6023587d90e7bb06a40858b7ccd6b58ae6 Mon Sep 17 00:00:00 2001 From: Alvaro Faundez Date: Mon, 12 Feb 2018 15:06:06 -0500 Subject: [PATCH 1/2] Refactor complete gem Remove configuration warnings, Fixes#2 Add test to whole site building Reorder gem files Refactor haml usage to one file --- Gemfile.lock | 7 +- README.md | 13 ++- jekyll-haml-markup.gemspec | 4 +- lib/jekyll-haml-markup.rb | 44 +-------- lib/jekyll/configuration.rb | 95 ------------------- lib/jekyll/converters/haml.rb | 34 ------- lib/jekyll/haml/markup.rb | 15 +++ lib/jekyll/haml/markup/converter.rb | 43 +++++++++ lib/jekyll/haml/markup/hooks.rb | 11 +++ lib/jekyll/{tags => haml/markup}/include.rb | 8 +- lib/jekyll/haml/{ => markup}/parser.rb | 0 .../haml/markup}/version.rb | 0 .../fixtures/sites/fake/_includes/footer.html | 3 + .../fixtures/sites/fake/_includes/header.haml | 4 + .../fixtures/sites/fake/_layouts/default.haml | 14 +++ test/fixtures/sites/fake/index.haml | 5 + test/jekyll/converters/haml_test.rb | 5 +- test/jekyll/haml/markup_test.rb | 28 +++++- test/test_helper.rb | 51 ++++++++++ 19 files changed, 196 insertions(+), 188 deletions(-) delete mode 100644 lib/jekyll/configuration.rb delete mode 100644 lib/jekyll/converters/haml.rb create mode 100644 lib/jekyll/haml/markup.rb create mode 100644 lib/jekyll/haml/markup/converter.rb create mode 100644 lib/jekyll/haml/markup/hooks.rb rename lib/jekyll/{tags => haml/markup}/include.rb (73%) rename lib/jekyll/haml/{ => markup}/parser.rb (100%) rename lib/{jekyll-haml-markup => jekyll/haml/markup}/version.rb (100%) create mode 100644 test/fixtures/sites/fake/_includes/footer.html create mode 100644 test/fixtures/sites/fake/_includes/header.haml create mode 100644 test/fixtures/sites/fake/_layouts/default.haml create mode 100644 test/fixtures/sites/fake/index.haml diff --git a/Gemfile.lock b/Gemfile.lock index d0dc435..4f14183 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,6 +4,7 @@ PATH jekyll-haml-markup (0.1.1) haml (~> 5.0) jekyll (~> 3.7) + liquid (~> 4.0) GEM remote: https://rubygems.org/ @@ -48,7 +49,10 @@ GEM rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) mercenary (0.3.6) + mini_portile2 (2.3.0) minitest (5.11.3) + nokogiri (1.8.2) + mini_portile2 (~> 2.3.0) pathutil (0.16.1) forwardable-extended (~> 2.6) public_suffix (3.0.1) @@ -56,7 +60,7 @@ GEM rb-fsevent (0.10.2) rb-inotify (0.9.10) ffi (>= 0.5.0, < 2) - rouge (3.1.0) + rouge (3.1.1) ruby_dep (1.5.0) safe_yaml (1.0.4) sass (3.5.5) @@ -74,6 +78,7 @@ DEPENDENCIES bundler (~> 1.16) jekyll-haml-markup! minitest (~> 5.0) + nokogiri (~> 1.6, >= 1.6.8) rake (~> 10.0) BUNDLED WITH diff --git a/README.md b/README.md index cd5407e..fe9dce9 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [Jekyll](https://jekyllrb.com) plugin that enables [Haml](http://haml.info) as a markup option for layouts and partials. -**Note**: This gem overrides a few jekyll methods and it lacks of testing. +**Note**: This gem overwrites the `include` jekyll tag. ## Installation @@ -22,14 +22,17 @@ The layout haml markup is rendered with hooks before the [Liquid](http://shopify !!! %html %head - {% assets style.css %} + %title Fake title + %link{href: "{{ '/assets/css/style.css?v=' | append: site.github.build_revision | relative_url }}", rel: "stylesheet", type: 'text/css'} + %link(href="{{ '/assets/css/style.css?v=' | append: site.github.build_revision | relative_url }}" rel="stylesheet" type='text/css') %body - %head - {% include head.haml %} + %header + {% include header.haml %} %main {{ content }} %footer - {% include footer.haml %} + {% include footer.html %} + %script{src: "{{ '/assets/javascript/script.css?v=' | append: site.github.build_revision | relative_url }}"} ``` Front-matter headers are not working on partials. diff --git a/jekyll-haml-markup.gemspec b/jekyll-haml-markup.gemspec index 314e10a..23ff2eb 100644 --- a/jekyll-haml-markup.gemspec +++ b/jekyll-haml-markup.gemspec @@ -1,7 +1,7 @@ lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'jekyll-haml-markup/version' +require 'jekyll/haml/markup/version' Gem::Specification.new do |spec| spec.name = "jekyll-haml-markup" @@ -24,7 +24,9 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.16" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "minitest", "~> 5.0" + spec.add_development_dependency 'nokogiri', '~> 1.6', '>= 1.6.8' spec.add_runtime_dependency 'jekyll', '~> 3.7' + spec.add_runtime_dependency 'liquid', '~> 4.0' spec.add_runtime_dependency 'haml', '~> 5.0' end diff --git a/lib/jekyll-haml-markup.rb b/lib/jekyll-haml-markup.rb index b66ad14..add2a55 100644 --- a/lib/jekyll-haml-markup.rb +++ b/lib/jekyll-haml-markup.rb @@ -1,43 +1 @@ -require 'jekyll-haml-markup/version' - -require 'jekyll' - -require_relative 'jekyll/converters/haml' -require_relative 'jekyll/haml/parser' -require_relative 'jekyll/configuration' -require_relative 'jekyll/tags/include' - - -Jekyll::Hooks.register :site, :post_read do |site| - haml_converter = Jekyll::Converters::Haml.new site.config - site.layouts.each do |name, layout| - if haml_converter.matches layout.ext - parsed_content = haml_converter.convert layout.content - layout.content = parsed_content - end - end -end - -# Jekyll::Hooks.register :pages, :pre_render do |page| -# haml_converter = Jekyll::Converters::Haml.new page.site.config -# if haml_converter.matches page.ext -# parsed_layout = haml_converter.convert page.content -# page.content = parsed_layout -# end -# end -# -# Jekyll::Hooks.register :posts, :pre_render do |post| -# haml_converter = Jekyll::Converters::Haml.new post.site.config -# if haml_converter.matches post.ext -# parsed_layout = haml_converter.convert post.content -# post.content = parsed_layout -# end -# end -# -# Jekyll::Hooks.register :documents, :pre_render do |document| -# haml_converter = Jekyll::Converters::Haml.new document.site.config -# if haml_converter.matches document.ext -# parsed_layout = haml_converter.convert document.content -# document.content = parsed_layout -# end -# end +require_relative 'jekyll/haml/markup' diff --git a/lib/jekyll/configuration.rb b/lib/jekyll/configuration.rb deleted file mode 100644 index 3cffa50..0000000 --- a/lib/jekyll/configuration.rb +++ /dev/null @@ -1,95 +0,0 @@ -require 'jekyll/configuration' -# frozen_string_literal: true - -module Jekyll - class Configuration < Hash - # Default options. Overridden by values in _config.yml. - # Strings rather than symbols are used for compatibility with YAML. - DEFAULTS = Configuration[{ - 'haml_ext' => 'haml', - 'haml' => 'lib-haml', - 'lib-haml' => { - attr_wrapper: '"', - escape_attrs: false - }, - - # Where things are - "source" => Dir.pwd, - "destination" => File.join(Dir.pwd, "_site"), - "collections_dir" => "", - "plugins_dir" => "_plugins", - "layouts_dir" => "_layouts", - "data_dir" => "_data", - "includes_dir" => "_includes", - "collections" => {}, - - # Handling Reading - "safe" => false, - "include" => [".htaccess"], - "exclude" => %w( - Gemfile Gemfile.lock node_modules vendor/bundle/ vendor/cache/ vendor/gems/ - vendor/ruby/ - ), - "keep_files" => [".git", ".svn"], - "encoding" => "utf-8", - "markdown_ext" => "markdown,mkdown,mkdn,mkd,md", - "strict_front_matter" => false, - - # Filtering Content - "show_drafts" => nil, - "limit_posts" => 0, - "future" => false, - "unpublished" => false, - - # Plugins - "whitelist" => [], - "plugins" => [], - - # Conversion - "markdown" => "kramdown", - "highlighter" => "rouge", - "lsi" => false, - "excerpt_separator" => "\n\n", - "incremental" => false, - - # Serving - "detach" => false, # default to not detaching the server - "port" => "4000", - "host" => "127.0.0.1", - "baseurl" => nil, # this mounts at /, i.e. no subdirectory - "show_dir_listing" => false, - - # Output Configuration - "permalink" => "date", - "paginate_path" => "/page:num", - "timezone" => nil, # use the local timezone - - "quiet" => false, - "verbose" => false, - "defaults" => [], - - "liquid" => { - "error_mode" => "warn", - }, - - "rdiscount" => { - "extensions" => [], - }, - - "redcarpet" => { - "extensions" => [], - }, - - "kramdown" => { - "auto_ids" => true, - "toc_levels" => "1..6", - "entity_output" => "as_char", - "smart_quotes" => "lsquo,rsquo,ldquo,rdquo", - "input" => "GFM", - "hard_wrap" => false, - "footnote_nr" => 1, - "show_warnings" => false, - }, - }.map { |k, v| [k, v.freeze] }].freeze - end -end diff --git a/lib/jekyll/converters/haml.rb b/lib/jekyll/converters/haml.rb deleted file mode 100644 index 8668fd1..0000000 --- a/lib/jekyll/converters/haml.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'jekyll/converter' -require 'jekyll/haml/parser' - -module Jekyll - module Converters - class Haml < Identity - safe true - - def extname_list - @extname_list ||= @config["haml_ext"].split(",").map do |e| - ".#{e.downcase}" - end - end - - def matches(ext) - extname_list.include?(ext.downcase) - end - - def output_ext(ext) - '.html' - end - - def get_processor - case @config["haml"].downcase - when "lib-haml" then return Jekyll::Haml::Parser.new(@config) - end - end - - def convert(content) - get_processor.convert content - end - end - end -end diff --git a/lib/jekyll/haml/markup.rb b/lib/jekyll/haml/markup.rb new file mode 100644 index 0000000..c77bc7b --- /dev/null +++ b/lib/jekyll/haml/markup.rb @@ -0,0 +1,15 @@ +require_relative 'markup/version' +require_relative 'markup/parser' +require_relative 'markup/converter' +require_relative 'markup/include' +require_relative 'markup/hooks' + +module Jekyll + module Haml + module Markup + def self.root + Pathname.new File.expand_path('../../../..', __FILE__) + end + end + end +end diff --git a/lib/jekyll/haml/markup/converter.rb b/lib/jekyll/haml/markup/converter.rb new file mode 100644 index 0000000..17952cd --- /dev/null +++ b/lib/jekyll/haml/markup/converter.rb @@ -0,0 +1,43 @@ +require 'jekyll' + +module Jekyll + module Converters + class Haml < Converter + + def extname_list + @extname_list ||= haml_conf["haml_ext"].split(",").map do |e| + ".#{e.downcase}" + end + end + + def matches(ext) + extname_list.include?(ext.downcase) + end + + def output_ext(ext) + '.html' + end + + def get_processor + case haml_conf['haml'].downcase + when 'lib-haml' then return Jekyll::Haml::Parser.new haml_conf + end + end + + def convert(content) + get_processor.convert content + end + + def haml_conf + { + 'haml_ext' => 'haml', + 'haml' => 'lib-haml', + 'lib-haml' => { + attr_wrapper: '"', + escape_attrs: false + } + } + end + end + end +end diff --git a/lib/jekyll/haml/markup/hooks.rb b/lib/jekyll/haml/markup/hooks.rb new file mode 100644 index 0000000..51d1566 --- /dev/null +++ b/lib/jekyll/haml/markup/hooks.rb @@ -0,0 +1,11 @@ +require 'jekyll' + +Jekyll::Hooks.register :site, :post_read do |site| + haml_converter = Jekyll::Converters::Haml.new site.config + site.layouts.each do |name, layout| + if haml_converter.matches layout.ext + parsed_content = haml_converter.convert layout.content + layout.content = parsed_content + end + end +end diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/haml/markup/include.rb similarity index 73% rename from lib/jekyll/tags/include.rb rename to lib/jekyll/haml/markup/include.rb index 847f269..cd87e03 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/haml/markup/include.rb @@ -1,9 +1,9 @@ -require 'jekyll/tags/include' -require 'jekyll/converters/haml' +require 'jekyll' +require 'liquid' module Jekyll module Tags - class IncludeTag < Liquid::Tag + class HamlInclude < IncludeTag def read_file(file, context) content = File.read file, file_read_opts(context) @@ -18,3 +18,5 @@ def read_file(file, context) end end end + +Liquid::Template.register_tag 'include', Jekyll::Tags::HamlInclude diff --git a/lib/jekyll/haml/parser.rb b/lib/jekyll/haml/markup/parser.rb similarity index 100% rename from lib/jekyll/haml/parser.rb rename to lib/jekyll/haml/markup/parser.rb diff --git a/lib/jekyll-haml-markup/version.rb b/lib/jekyll/haml/markup/version.rb similarity index 100% rename from lib/jekyll-haml-markup/version.rb rename to lib/jekyll/haml/markup/version.rb diff --git a/test/fixtures/sites/fake/_includes/footer.html b/test/fixtures/sites/fake/_includes/footer.html new file mode 100644 index 0000000..48b54f4 --- /dev/null +++ b/test/fixtures/sites/fake/_includes/footer.html @@ -0,0 +1,3 @@ + diff --git a/test/fixtures/sites/fake/_includes/header.haml b/test/fixtures/sites/fake/_includes/header.haml new file mode 100644 index 0000000..a9bb20e --- /dev/null +++ b/test/fixtures/sites/fake/_includes/header.haml @@ -0,0 +1,4 @@ +%nav + %ul + %li + %a{href: "/"} Fake link diff --git a/test/fixtures/sites/fake/_layouts/default.haml b/test/fixtures/sites/fake/_layouts/default.haml new file mode 100644 index 0000000..e5afc83 --- /dev/null +++ b/test/fixtures/sites/fake/_layouts/default.haml @@ -0,0 +1,14 @@ +!!! +%html + %head + %title Fake title + %link{href: "{{ '/assets/css/style.css?v=' | append: site.github.build_revision | relative_url }}", rel: "stylesheet", type: 'text/css'} + %link(href="{{ '/assets/css/style.css?v=' | append: site.github.build_revision | relative_url }}" rel="stylesheet" type='text/css') + %body + %header + {% include header.haml %} + %main + {{ content }} + %footer + {% include footer.html %} + %script{src: "{{ '/assets/javascript/script.css?v=' | append: site.github.build_revision | relative_url }}"} diff --git a/test/fixtures/sites/fake/index.haml b/test/fixtures/sites/fake/index.haml new file mode 100644 index 0000000..bf85287 --- /dev/null +++ b/test/fixtures/sites/fake/index.haml @@ -0,0 +1,5 @@ +--- +layout: default +--- +%h1 Fake Heading +%p Fake paragraph diff --git a/test/jekyll/converters/haml_test.rb b/test/jekyll/converters/haml_test.rb index c0f2b69..ce1d1a8 100644 --- a/test/jekyll/converters/haml_test.rb +++ b/test/jekyll/converters/haml_test.rb @@ -1,9 +1,8 @@ require 'test_helper' -require 'liquid' describe Haml do before do - @haml_converter = Jekyll::Converters::Haml.new Jekyll::Configuration.from({}) + @haml_converter = Jekyll::Converters::Haml.new end it 'should render html' do @@ -11,7 +10,7 @@ @haml_converter.convert("%p test\n").must_equal "

test

\n" end - it "render with" do + it "should render with escaped quotes" do input = <<~HAML %link(href="{{ '/assets/css/style.css?v=\\\\\\"12345\\\\\\"' }}" rel="stylesheet" type="text/css") HAML diff --git a/test/jekyll/haml/markup_test.rb b/test/jekyll/haml/markup_test.rb index d434728..7b6f1c2 100644 --- a/test/jekyll/haml/markup_test.rb +++ b/test/jekyll/haml/markup_test.rb @@ -1,7 +1,29 @@ require 'test_helper' +require 'nokogiri' -class Jekyll::Haml::MarkupTest < Minitest::Test - def test_that_it_has_a_version_number - refute_nil ::Jekyll::Haml::Markup::VERSION +describe Jekyll::Haml::Markup do + before do + options = {} + options['source'] = source_dir('sites', 'fake') + @destination = Dir.mktmpdir ['fake'], dest_dir + options['destination'] = @destination + config = Jekyll::Configuration::DEFAULTS.merge options + @site = fixture_site config + @site.process + end + + after do + FileUtils.remove_entry @destination + end + + it 'should load a complete site' do + xslt = Nokogiri::XSLT xls + html = Nokogiri::HTML File.read File.join(@destination, 'index.html') + html.xpath('/html/head').wont_be_empty + html.xpath('/html/body').wont_be_empty + html.xpath('/html/body/header/nav/ul/li/a').wont_be_empty + html.xpath('/html/body/main/h1').wont_be_empty + html.xpath('/html/body/footer/div').wont_be_empty + html.xpath('/html/body/script').wont_be_empty end end diff --git a/test/test_helper.rb b/test/test_helper.rb index e022069..bc8b432 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,3 +2,54 @@ require 'jekyll-haml-markup' require 'minitest/autorun' + + +def root_dir(*subdirs) + File.expand_path(File.join('..', *subdirs), __dir__) +end + +def test_dir(*subdirs) + root_dir('test', *subdirs) +end + +def source_dir(*subdirs) + test_dir('fixtures', *subdirs) +end + +def dest_dir(*subdirs) + test_dir('tmp', *subdirs) +end + +def default_configuration + Marshal.load(Marshal.dump(Jekyll::Configuration::DEFAULTS)) +end + +def build_configs(overrides, base_hash = default_configuration) + Jekyll::Utils.deep_merge_hashes(base_hash, overrides) +end + +def site_configuration(overrides = {}) + full_overrides = build_configs(overrides, build_configs({ + "destination" => dest_dir, + "incremental" => false, + })) + # Jekyll::Configuration.from(full_overrides.merge({ + # "source" => source_dir, + # })) +end + +def fixture_site(overrides = {}) + Jekyll::Site.new(site_configuration(overrides)) +end + +def xls + <<-XLS + + + + + + + + XLS +end From 56a2a84e4fca22cda0524c4e0fb7d43879110f9e Mon Sep 17 00:00:00 2001 From: Alvaro Faundez Date: Mon, 12 Feb 2018 15:09:51 -0500 Subject: [PATCH 2/2] Add test tmp dir --- test/.tmp/.gitkeep | 0 test/test_helper.rb | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 test/.tmp/.gitkeep diff --git a/test/.tmp/.gitkeep b/test/.tmp/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/test_helper.rb b/test/test_helper.rb index bc8b432..85cd9cb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,7 +17,7 @@ def source_dir(*subdirs) end def dest_dir(*subdirs) - test_dir('tmp', *subdirs) + test_dir('.tmp', *subdirs) end def default_configuration