Skip to content

Commit

Permalink
Merge markdown_processor
Browse files Browse the repository at this point in the history
  • Loading branch information
imtodor committed Dec 11, 2018
2 parents d84f4a0 + 8637f9e commit 81d4582
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 47 deletions.
2 changes: 2 additions & 0 deletions _data/support_products.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
"Telerik UI for Xamarin": 1534
"Telerik UI for ASP.NET Core": 1725
"Telerik UI for ASP.NET MVC": 982
"Progress Telerik Fiddler": 1392
"Progress Telerik Fiddler Everywhere": 1892
124 changes: 77 additions & 47 deletions _plugins/markdown_processor.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
module Jekyll

require 'html/pipeline'
require 'html/pipeline'

class LangFilter < HTML::Pipeline::Filter
class LangFilter < HTML::Pipeline::Filter

def call
doc.css('pre[lang]').each do |node|
node['data-lang'] = node['lang']
node.remove_attribute('lang')
end

doc
end
def call
doc.css('pre[lang]').each do |node|
node['data-lang'] = node['lang']
node.remove_attribute('lang')
end

doc
end
end

class ApiHeaderIdFilter < HTML::Pipeline::Filter

Expand Down Expand Up @@ -65,60 +64,91 @@ def sanitaze_id(id)
end


class HeaderLinkFilter < HTML::Pipeline::Filter
class RootRelativeFilter < HTML::Pipeline::Filter

PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u
def call
doc.search('a').each do |a|
next if a['href'].nil?

def call()
href = a['href'].strip

doc.css('h1, h2, h3').each do |heading|
if href.start_with? '/'
a['href'] = context[:baseurl] + href unless href.start_with? context[:baseurl]
end
end

doc.search('img').each do |img|
next if img['src'].nil?

desc_node = heading.children.first()
if desc_node
id = desc_node.text
else
id = heading.text
end
src = img['src'].strip

if src.start_with? '/'
img['src'] = context[:baseurl] + src unless src.start_with? context[:baseurl]
end
end

id = id.downcase.strip
id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
id.gsub!(' ', '-') # replace spaces with dash
doc
end
end

heading['id'] = id
class HeaderLinkFilter < HTML::Pipeline::Filter

a = Nokogiri::XML::Node.new('a', doc)
a['href'] = "##{id}"
PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u

if desc_node
a.children = desc_node
end
def call()

if next_child = heading.children.first()
next_child.before(a)
else
heading.add_child a
end
doc.css('h1, h2, h3').each do |heading|

desc_node = heading.children.first()
if desc_node
id = desc_node.text
else
id = heading.text
end

doc
end
end
id = id.downcase.strip
id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
id.gsub!(' ', '-') # replace spaces with dash

class Converters::Markdown::MarkdownProcessor
def initialize(config)
@config = config
heading['id'] = id

if @config['code_lang']
@pipeline = HTML::Pipeline.new [ HTML::Pipeline::MarkdownFilter, LangFilter, ApiHeaderIdFilter, HeaderLinkFilter ]
else
@pipeline = HTML::Pipeline.new [ HTML::Pipeline::MarkdownFilter, ApiHeaderIdFilter, HeaderLinkFilter ]
a = Nokogiri::XML::Node.new('a', doc)
a['href'] = "##{id}"

if desc_node
a.children = desc_node
end

if next_child = heading.children.first()
next_child.before(a)
else
heading.add_child a
end
end

def convert(content)
@pipeline.call(content)[:output].to_s
end

doc
end
end

class Converters::Markdown::MarkdownProcessor
def initialize(config)
@config = config
context = {
:gfm => false,
:baseurl => @config['baseurl'],
}

if @config['code_lang']
@pipeline = HTML::Pipeline.new [ HTML::Pipeline::MarkdownFilter, LangFilter, RootRelativeFilter, HeaderLinkFilter ], context
else
@pipeline = HTML::Pipeline.new [ HTML::Pipeline::MarkdownFilter, RootRelativeFilter, HeaderLinkFilter ], context
end
end

def convert(content)
@pipeline.call(content)[:output].to_s
end
end

end

0 comments on commit 81d4582

Please sign in to comment.