Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ Gemfile.lock
packaging/builds
.vscode
*.DS_Store

# Theme Liquid docs live at git@github.com:Shopify/theme-liquid-docs.git,
/data/shopify_liquid/documentation/
5 changes: 4 additions & 1 deletion lib/theme_check/shopify_liquid.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

require_relative 'shopify_liquid/deprecated_filter'
require_relative 'shopify_liquid/documentation'
require_relative 'shopify_liquid/filter'
require_relative 'shopify_liquid/object'
require_relative 'shopify_liquid/tag'
require_relative 'shopify_liquid/source_index'
require_relative 'shopify_liquid/system_translations'
require_relative 'shopify_liquid/tag'
44 changes: 44 additions & 0 deletions lib/theme_check/shopify_liquid/documentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require_relative 'documentation/markdown_template'

module ThemeCheck
module ShopifyLiquid
class Documentation
class << self
def filter_doc(filter_name)
render_doc(SourceIndex.filters.find { |entry| entry.name == filter_name })
end

def object_doc(object_name)
render_doc(SourceIndex.objects.find { |entry| entry.name == object_name })
end

def tag_doc(tag_name)
render_doc(SourceIndex.tags.find { |entry| entry.name == tag_name })
end

def object_property_doc(object_name, property_name)
property_entry = SourceIndex
.objects
.find { |entry| entry.name == object_name }
&.properties
&.find { |prop| prop.name == property_name }

render_doc(property_entry)
end

private

def render_doc(entry)
return nil unless entry
markdown_template.render(entry)
end

def markdown_template
@markdown_template ||= MarkdownTemplate.new
end
end
end
end
end
33 changes: 33 additions & 0 deletions lib/theme_check/shopify_liquid/documentation/markdown_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module ThemeCheck
module ShopifyLiquid
class Documentation
class MarkdownTemplate
def render(entry)
[
title(entry),
body(entry),
].reject(&:empty?).join("\n")
end

private

def title(entry)
"### #{entry.name}"
end

def body(entry)
[entry.summary, entry.description]
.reject(&:nil?)
.reject(&:empty?)
.join(horizontal_rule)
end

def horizontal_rule
"\n---\n"
end
end
end
end
end
73 changes: 73 additions & 0 deletions lib/theme_check/shopify_liquid/source_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require 'json'
require 'pathname'

require_relative 'source_index/base_entry'
require_relative 'source_index/filter_entry'
require_relative 'source_index/object_entry'
require_relative 'source_index/parameter_entry'
require_relative 'source_index/property_entry'
require_relative 'source_index/return_type_entry'
require_relative 'source_index/tag_entry'

module ThemeCheck
module ShopifyLiquid
class SourceIndex
TYPE_SOURCE = Pathname.new("#{__dir__}/../../../data/shopify_liquid/documentation")

class << self
def filters
@filters ||= load_file(:filters).map { |hash| SourceIndex::FilterEntry.new(hash) }
end

def objects
@objects ||= load_file(:objects).map { |hash| SourceIndex::ObjectEntry.new(hash) }
end

def tags
@tags ||= load_file(:tags).map { |hash| SourceIndex::TagEntry.new(hash) }
end

private

def load_file(file_name)
read_json(file_path(file_name))
end

def read_json(path)
download_files unless has_files?

JSON.parse(path.read)
end

def download_files
################################################################################
## TODO (REMOVE ME):
## Remove this implementation in favor of a proper/stable approach
## to download/update theme-liquid-docs files.
################################################################################
commands = [
'git clone git@github.com:Shopify/theme-liquid-docs.git /tmp/theme-liquid-docs-tmp',
'cd /tmp/theme-liquid-docs-tmp',
'git reset origin/init-repo --hard',
"mv data/filters.json #{__dir__}/../../../data/shopify_liquid/documentation",
"mv data/objects.json #{__dir__}/../../../data/shopify_liquid/documentation",
"mv data/tags.json #{__dir__}/../../../data/shopify_liquid/documentation",
'cd -',
].join(' && ')

Kernel.exec(commands)
end

def has_files?
[:filters, :objects, :tags].all? { |file_name| file_path(file_name).exist? }
end

def file_path(file_name)
TYPE_SOURCE + "#{file_name}.json"
end
end
end
end
end
41 changes: 41 additions & 0 deletions lib/theme_check/shopify_liquid/source_index/base_entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module ThemeCheck
module ShopifyLiquid
class SourceIndex
class BaseEntry
attr_reader :hash

def initialize(hash = {})
@hash = hash
end

def name
hash['name']
end

def summary
hash['summary']
end

def description
hash['description']
end

def return_type
return_type_instance.to_s
end

def return_type_instance
ReturnTypeEntry.new(return_type_hash)
end

private

def return_type_hash
hash['return_type']&.first
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/theme_check/shopify_liquid/source_index/filter_entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module ThemeCheck
module ShopifyLiquid
class SourceIndex
class FilterEntry < BaseEntry
def parameters
(hash['parameters'] || [])
.map { |hash| ParameterEntry.new(hash) }
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/theme_check/shopify_liquid/source_index/object_entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module ThemeCheck
module ShopifyLiquid
class SourceIndex
class ObjectEntry < BaseEntry
def properties
(hash['properties'] || [])
.map { |hash| PropertyEntry.new(hash) }
end
end
end
end
end
21 changes: 21 additions & 0 deletions lib/theme_check/shopify_liquid/source_index/parameter_entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module ThemeCheck
module ShopifyLiquid
class SourceIndex
class ParameterEntry < BaseEntry
def summary
nil
end

private

def return_type_hash
{
'type' => (hash['types'] || ['untyped']).first,
}
end
end
end
end
end
9 changes: 9 additions & 0 deletions lib/theme_check/shopify_liquid/source_index/property_entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module ThemeCheck
module ShopifyLiquid
class SourceIndex
class PropertyEntry < BaseEntry; end
end
end
end
33 changes: 33 additions & 0 deletions lib/theme_check/shopify_liquid/source_index/return_type_entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module ThemeCheck
module ShopifyLiquid
class SourceIndex
class ReturnTypeEntry < BaseEntry
def summary
nil
end

def to_s
hash['type']
end

def array_type?
!hash['array_value'].empty?
end

def array_type
hash['array_value']
end

private

def return_type_hash
{
'type' => "type<#{self}>",
}
end
end
end
end
end
20 changes: 20 additions & 0 deletions lib/theme_check/shopify_liquid/source_index/tag_entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module ThemeCheck
module ShopifyLiquid
class SourceIndex
class TagEntry < BaseEntry
def parameters
(hash['parameters'] || [])
.map { |hash| ParameterEntry.new(hash) }
end

def return_type_hash
{
'type' => "tag<#{name}>",
}
end
end
end
end
end
57 changes: 57 additions & 0 deletions test/shopify_liquid/documentation/markdown_template_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require 'test_helper'

module ThemeCheck
module ShopifyLiquid
class Documentation
class MarkdownTemplateTest < Minitest::Test
def setup
@markdown_template = MarkdownTemplate.new
end

def test_render
entry = SourceIndex::BaseEntry.new(
'name' => 'product',
'summary' => 'A product in the store.',
'description' => 'A more detailed description of a product in the store.',
)

actual_temaplte = @markdown_template.render(entry)
expected_template = "### product\n" \
"A product in the store.\n" \
"---\n" \
"A more detailed description of a product in the store."

assert_equal(expected_template, actual_temaplte)
end

def test_render_with_summary_only
entry = SourceIndex::BaseEntry.new(
'name' => 'product',
'summary' => 'A product in the store.'
)

actual_temaplte = @markdown_template.render(entry)
expected_template = "### product\n" \
"A product in the store." \

assert_equal(expected_template, actual_temaplte)
end

def test_render_with_description_only
entry = SourceIndex::BaseEntry.new(
'name' => 'product',
'description' => 'A more detailed description of a product in the store.'
)

actual_temaplte = @markdown_template.render(entry)
expected_template = "### product\n" \
"A more detailed description of a product in the store." \

assert_equal(expected_template, actual_temaplte)
end
end
end
end
end
Loading