Skip to content

Commit

Permalink
Switch gears on experimental component functionality (#5)
Browse files Browse the repository at this point in the history
* Switch gears on experimental component functionality

Now based on Shopify's new Render tag

* Fix for CI build

* Temporarily disable default-site test in CI

* Add support for multiple components paths

* Update changelog and roadmap, releasing 0.10.0
  • Loading branch information
jaredcwhite authored Apr 18, 2020
1 parent 526ab49 commit 7467cc6
Show file tree
Hide file tree
Showing 25 changed files with 174 additions and 80 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# master

# 0.10.0 / 2020-04-17

**Switch gears on _experimental_ component functionality.**

Going with a new `rendercontent` tag instead of `component`. It is based on
Shopify's new Render tag which recently got introduced to Liquid. Note that the
feature hasn't been officially released via the Liquid gem, so we need to use the
master branch that's been forked on GitHub with a higher version number).

[#5](https://github.com/bridgetownrb/bridgetown/pull/5)

# 0.9.0 / 2020-04-16

* Update table styling in Documentation
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ group :bridgetown_optional_dependencies do
gem "tomlrb", "~> 1.2"
gem "classifier-reborn", "~> 2.2"
gem "liquid-c", "~> 4.0"
# Pull in latest Liquid from Shopify with new Render tag
gem 'liquid', "> 4.0.3", github: "jaredcwhite/liquid"
gem "yajl-ruby", "~> 1.4"
end

Expand Down
3 changes: 3 additions & 0 deletions bridgetown-core/lib/bridgetown-core/commands/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def gemfile_contents
#
# Happy Bridgetowning!
# Pull in latest Liquid from Shopify with new Render tag
gem 'liquid', "> 4.0.3", github: "jaredcwhite/liquid"
gem "bridgetown", "~> #{Bridgetown::VERSION}"
RUBY
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/lib/bridgetown-core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Configuration < Hash
"cache_dir" => ".bridgetown-cache",
"layouts_dir" => "_layouts",
"data_dir" => "_data",
"components_dir" => "_components",
"includes_dir" => "_includes",
"collections" => {},

Expand Down
7 changes: 7 additions & 0 deletions bridgetown-core/lib/bridgetown-core/liquid_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "liquid_renderer/file_system"
require_relative "liquid_renderer/file"
require_relative "liquid_renderer/table"

Expand All @@ -11,6 +12,12 @@ class LiquidRenderer

def initialize(site)
@site = site

# Set up Liquid file system access to components for the Render tag
Liquid::Template.file_system = LiquidRenderer::FileSystem.new(
@site.components_load_paths, "%s.liquid"
)

Liquid::Template.error_mode = @site.config["liquid"]["error_mode"].to_sym
reset
end
Expand Down
7 changes: 6 additions & 1 deletion bridgetown-core/lib/bridgetown-core/liquid_renderer/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ def initialize(renderer, filename)

def parse(content)
measure_time do
@renderer.cache[@filename] ||= Liquid::Template.parse(content, line_numbers: true)
# Remove extraneous indentation for rendercontent tags
processed_content = content.gsub(%r!^[ \t]+{%-? rendercontent!, "{% rendercontent")

@renderer.cache[@filename] ||= Liquid::Template.parse(
processed_content, line_numbers: true
)
end
@template = @renderer.cache[@filename]

Expand Down
36 changes: 36 additions & 0 deletions bridgetown-core/lib/bridgetown-core/liquid_renderer/file_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module Bridgetown
class LiquidRenderer
class FileSystem < Liquid::LocalFileSystem
def read_template_file(template_path)
load_paths = root
found_paths = []

load_paths.each do |load_path|
# Use Liquid's gut checks to verify template pathname
self.root = load_path
full_template_path = full_path(template_path)

# Look for .liquid as well as .html extensions
path_variants = [
Pathname.new(full_template_path),
Pathname.new(full_template_path).sub_ext(".html"),
]

found_paths << path_variants.find(&:exist?)
end

# Restore pristine state
self.root = load_paths

found_paths.compact!

raise Liquid::FileSystemError, "No such template '#{template_path}'" if found_paths.empty?

# Last path in the list wins
::File.read(found_paths.last)
end
end
end
end
18 changes: 17 additions & 1 deletion bridgetown-core/lib/bridgetown-core/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Site
:plugin_manager

attr_accessor :converters, :generators, :reader
attr_reader :regenerator, :liquid_renderer, :includes_load_paths
attr_reader :regenerator, :liquid_renderer, :components_load_paths,
:includes_load_paths

# Public: Initialize a new Site.
#
Expand Down Expand Up @@ -52,6 +53,7 @@ def config=(config)

configure_cache
configure_plugins
configure_component_paths
configure_include_paths
configure_file_read_opts

Expand Down Expand Up @@ -435,6 +437,20 @@ def configure_plugins
self.plugins = plugin_manager.plugins_path
end

def configure_component_paths
@components_load_paths = config["components_dir"].then do |dir|
dir.is_a?(Array) ? dir : [dir]
end
@components_load_paths.map! do |dir|
if !!(dir =~ %r!^\.\.?\/!)
# allow ./dir or ../../dir type options
File.expand_path(dir.to_s, root_dir)
else
in_source_dir(dir.to_s)
end
end
end

def configure_include_paths
@includes_load_paths = Array(in_source_dir(config["includes_dir"].to_s))
end
Expand Down
34 changes: 0 additions & 34 deletions bridgetown-core/lib/bridgetown-core/tags/component.rb

This file was deleted.

14 changes: 3 additions & 11 deletions bridgetown-core/lib/bridgetown-core/tags/include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,9 @@ def tag_includes_dirs(context)

def locate_include_file(context, file)
includes_dirs = tag_includes_dirs(context)
# TODO: component folder should be configured separately
if file.start_with?("_components")
first_dir = includes_dirs.first
path = PathManager.join(first_dir.sub(%r!/_includes$!, ""), file)
return path if valid_include_file?(path, first_dir.to_s)
else
includes_dirs.each do |dir|
path = PathManager.join(dir, file)
return path if valid_include_file?(path, dir.to_s)
end
includes_dirs.each do |dir|
path = PathManager.join(dir, file)
return path if valid_include_file?(path, dir.to_s)
end
raise IOError, could_not_locate_message(file, includes_dirs)
end
Expand Down Expand Up @@ -184,7 +177,6 @@ def read_file(file, context)

private

# TODO: update this to support components as well
def could_not_locate_message(file, includes_dirs)
"Could not locate the included file '#{file}' in any of #{includes_dirs}." \
" Ensure it exists in one of those directories."
Expand Down
32 changes: 32 additions & 0 deletions bridgetown-core/lib/bridgetown-core/tags/render_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Bridgetown
module Tags
class BlockRenderTag < Liquid::Block
def initialize(tag_name, markup, options)
super

@tag = tag_name
@markup = markup
@options = options
end

def render(context)
content = super.gsub(%r!^[ \t]+!, "") # unindent the incoming text

site = context.registers[:site]
converter = site.find_converter_instance(Bridgetown::Converters::Markdown)
markdownified_content = converter.convert(content)

context.stack do
context["componentcontent"] = markdownified_content
render_params = "#{@markup}, content: componentcontent"
render_tag = Liquid::Render.parse("render", render_params, @options, @parse_context)
render_tag.render_tag(context, +"")
end
end
end
end
end

Liquid::Template.register_tag("rendercontent", Bridgetown::Tags::BlockRenderTag)
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Bridgetown
VERSION = "0.9.0"
VERSION = "0.10.0"
end
3 changes: 2 additions & 1 deletion bridgetown-core/script/cibuild
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ then
script/fmt
script/test
script/cucumber
script/default-site
# NOTE: Failing in CI due to Liquid gem version issues...
# script/default-site
elif [[ -x "script/$TEST_SUITE" ]]
then
script/$TEST_SUITE
Expand Down
10 changes: 2 additions & 8 deletions bridgetown-core/test/source/src/_components/test_component.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<span id='include-param'>{{include.param}}</span>
<span id='include-param'>{{param}}</span>

<ul id='param-list'>
{% for param in include %}
<li>{{param[0]}} = {{param[1]}}</li>
{% endfor %}
</ul>

{{ include.content}}
<main>{{ content}}</main>
12 changes: 6 additions & 6 deletions bridgetown-core/test/test_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1050,17 +1050,17 @@ def highlight_block_with_opts(options_string)
end
end

context "component tag" do
context "rendercontent tag" do
context "with one parameter" do
setup do
content = <<~CONTENT
---
title: Component tag parameters
title: Tag parameters
---
{% component test_component param="value" %}
{% rendercontent "test_component", param: "value" %}
* I am Markdown
{% endcomponent %}
{% endrendercontent %}
CONTENT
create_post(content,
"permalink" => "pretty",
Expand All @@ -1070,8 +1070,8 @@ def highlight_block_with_opts(options_string)
end

should "correctly output params and markdown content" do
assert_match "<span id=\"include-param\">value</span>", @result.strip
assert_match "<li>I am Markdown</li>", @result.strip
assert_match "<span id=\"include-param\">value</span>", @result
assert_match "<main>\n<ul>\n <li>I am Markdown</li>\n</ul>\n</main>", @result
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions bridgetown-website/Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
source "https://rubygems.org"

# Pull in latest Liquid from Shopify with new Render tag
gem 'liquid', "> 4.0.3", github: "jaredcwhite/liquid"

gem 'bridgetown-core', path: '../bridgetown-core'
gem 'bridgetown-paginate', path: '../bridgetown-paginate'
4 changes: 4 additions & 0 deletions bridgetown-website/bridgetown.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ permalink: simple

timezone: America/Los_Angeles

# components_dir:
# - ../shared/components
# - _components

collections:
docs:
output: true
Expand Down
11 changes: 11 additions & 0 deletions bridgetown-website/src/_components/docs/note.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{%- if extra_margin -%}
{%- assign extra_margin_class = "my-10" -%}
{%- endif -%}

<div class="note {{ type }} {{ extra_margin_class }}">
{%- if title -%}
<h5>{{ title }}</h5>
{%- endif -%}

{{- content -}}
</div>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ul>
{% assign current_section = "" %}
{% for doc_page in site.docs %}
{% for doc_page in docs %}
{% assign active = "" %}
{% if doc_page.top_section and current_section != doc_page.top_section %}
{% assign current_section = doc_page.top_section %}
Expand All @@ -13,4 +13,4 @@
<li><a href="{{ doc_page.url }}" class="{{ active }}">{{ doc_page.title }}</a></li>
{% endunless %}
{% endfor %}
</ul>
</ul>
27 changes: 17 additions & 10 deletions bridgetown-website/src/_docs/liquid/tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,23 @@ language identifier. To find the appropriate identifier to use for the language
you want to highlight, look for the “short name” on the [Rouge
wiki](https://github.com/jayferd/rouge/wiki/List-of-supported-languages-and-lexers).

<div class="note">
<h5>Bridgetown processes all Liquid filters in code blocks</h5>
<p>If you are using a language that contains curly braces, you
will likely need to place <code>{&#37; raw &#37;}</code> and
<code>{&#37; endraw &#37;}</code> tags around your code.
If needed, you can add <code>render_with_liquid: false</code> in your front matter to disable Liquid entirely for a particular document.</p>
</div>

{:.note}
You can also use fenced code blocks in Markdown (starting and ending with three backticks <code>```</code>) instead of using the `highlight` tag. However, the `highlight` tag includes additional features like line numbers (see below).
{% rendercontent "docs/note",
type: "warning",
extra_margin: true,
title: "Bridgetown processes all Liquid filters in code blocks" %}

If you are using a language that contains curly braces, you will likely need to
place <code>{&#37; raw &#37;}</code> and <code>{&#37; endraw &#37;}</code> tags
around your code. If needed, you can add `render_with_liquid: false` in your
front matter to disable Liquid entirely for a particular document.
{% endrendercontent %}

{% rendercontent "docs/note" %}
You can also use fenced code blocks in Markdown (starting and ending with three
backticks <code>```</code>) instead of using the `highlight` tag. However, the
`highlight` tag includes additional features like line numbers (see below).
{% endrendercontent %}


### Line numbers

Expand Down
Loading

0 comments on commit 7467cc6

Please sign in to comment.