Skip to content

Commit

Permalink
Merge pull request #56 from ind1go/filename-context
Browse files Browse the repository at this point in the history
Pass filename through to HTML pipeline
  • Loading branch information
gjtorikian committed Jun 29, 2018
2 parents 1877824 + fed3feb commit 674e244
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
source 'https://rubygems.org'

gem 'commonmarker', '~> 0.16', require: false
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'bundler/gem_tasks'
require 'rake/testtask'

Expand Down
1 change: 1 addition & 0 deletions graphql-docs.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding: utf-8
# frozen_string_literal: true
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'graphql-docs/version'
Expand Down
1 change: 1 addition & 0 deletions lib/graphql-docs.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'graphql-docs/helpers'
require 'graphql-docs/renderer'
require 'graphql-docs/configuration'
Expand Down
1 change: 1 addition & 0 deletions lib/graphql-docs/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module GraphQLDocs
module Configuration
GRAPHQLDOCS_DEFAULTS = {
Expand Down
6 changes: 4 additions & 2 deletions lib/graphql-docs/generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'erb'
require 'sass'

Expand Down Expand Up @@ -210,8 +211,9 @@ def write_file(type, name, contents, trim: true)
contents.gsub!(/^\s{4}/m, ' ')
end

contents = @renderer.render(contents, type: type, name: name)
File.write(File.join(path, 'index.html'), contents) unless contents.nil?
filename = File.join(path, 'index.html')
contents = @renderer.render(contents, type: type, name: name, filename: filename)
File.write(filename, contents) unless contents.nil?
end
end
end
1 change: 1 addition & 0 deletions lib/graphql-docs/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module GraphQLDocs
module Helpers
SLUGIFY_PRETTY_REGEXP = Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze
Expand Down
1 change: 1 addition & 0 deletions lib/graphql-docs/parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'graphql'

module GraphQLDocs
Expand Down
11 changes: 6 additions & 5 deletions lib/graphql-docs/renderer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'html/pipeline'
require 'yaml'
require 'extended-markdown-filter'
Expand Down Expand Up @@ -36,17 +37,17 @@ def initialize(parsed_schema, options)
@pipeline = HTML::Pipeline.new(filters, @pipeline_config[:context])
end

def render(contents, type: nil, name: nil)
opts = { base_url: @options[:base_url] }.merge({ type: type, name: name}).merge(helper_methods)
def render(contents, type: nil, name: nil, filename: nil)
opts = { base_url: @options[:base_url], output_dir: @options[:output_dir] }.merge({ type: type, name: name, filename: filename}).merge(helper_methods)

contents = to_html(contents)
contents = to_html(contents, context: { filename: filename })
return contents if @graphql_default_layout.nil?
opts[:content] = contents
@graphql_default_layout.result(OpenStruct.new(opts).instance_eval { binding })
end

def to_html(string)
@pipeline.to_html(string)
def to_html(string, context: {})
@pipeline.to_html(string, context)
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/graphql-docs/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module GraphQLDocs
VERSION = '1.4.1'
end
3 changes: 2 additions & 1 deletion test/graphql-docs/generator_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true
require 'test_helper'

class GeneratorTest < Minitest::Test
class CustomRenderer
def initialize(_, _)
end

def render(contents, type: nil, name: nil)
def render(contents, type: nil, name: nil, filename: nil)
to_html(contents)
end

Expand Down
1 change: 1 addition & 0 deletions test/graphql-docs/graphql-docs_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'test_helper'

class GraphQLDocsTest < Minitest::Test
Expand Down
1 change: 1 addition & 0 deletions test/graphql-docs/parser_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'test_helper'

class ParserTest < Minitest::Test
Expand Down
32 changes: 32 additions & 0 deletions test/graphql-docs/renderer_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'test_helper'

class RendererTest < Minitest::Test
Expand All @@ -19,4 +20,35 @@ def test_that_html_conversion_works

assert_equal '<p><strong>R2D2</strong></p>', contents
end

def test_that_filename_accessible_to_filters
@renderer = GraphQLDocs::Renderer.new(@parsed_schema, GraphQLDocs::Configuration::GRAPHQLDOCS_DEFAULTS.merge({
pipeline_config: {
pipeline:
%i(ExtendedMarkdownFilter
EmojiFilter
TableOfContentsFilter
AddFilenameFilter),
context: {
gfm: false,
asset_root: 'https://a248.e.akamai.net/assets.github.com/images/icons'
}
}
}))
contents = @renderer.render('<span id="fill-me"></span>', type: 'Droid', name: 'R2D2', filename: '/this/is/the/filename')
assert_match %r{<span id="fill-me">/this/is/the/filename</span>}, contents
end
end

class AddFilenameFilter < HTML::Pipeline::Filter
def call
doc.search('span[@id="fill-me"]').each do |span|
span.inner_html=(context[:filename])
end
doc
end

def validate
needs :filename
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'graphql-docs'

Expand Down

0 comments on commit 674e244

Please sign in to comment.