feat: add excluding content to skip markdown code

This commit is contained in:
jeffreytse 2020-04-29 13:08:04 +08:00
parent 169bdf1343
commit 7b21c0640b
2 changed files with 48 additions and 0 deletions

View File

@ -7,6 +7,7 @@ module Jekyll::Spaceship
@@_hooks = {} @@_hooks = {}
@@_registers = [] @@_registers = []
@@_processers = [] @@_processers = []
@@_exclusions = []
@@_priority = nil @@_priority = nil
attr_accessor :priority attr_accessor :priority
@ -40,6 +41,7 @@ module Jekyll::Spaceship
def initialize() def initialize()
self.initialize_priority self.initialize_priority
self.initialize_register self.initialize_register
self.initialize_exclusions
end end
def initialize_priority def initialize_priority
@ -71,6 +73,14 @@ module Jekyll::Spaceship
@@_registers.clear @@_registers.clear
end end
def initialize_exclusions
if @@_exclusions.size.zero?
self.class.exclude :code, :block_quotes
end
@_exclusions = @@_exclusions.uniq
@@_exclusions.clear
end
def self.priority(value) def self.priority(value)
value = value.to_sym value = value.to_sym
if PRIORITY_MAP.has_key? value if PRIORITY_MAP.has_key? value
@ -145,7 +155,9 @@ module Jekyll::Spaceship
end end
if self.respond_to? method if self.respond_to? method
@page.content = self.pre_exclude @page.content
@page.content = self.send method, @page.content @page.content = self.send method, @page.content
@page.content = self.after_exclude @page.content
end end
else else
if html? output_ext if html? output_ext
@ -162,6 +174,10 @@ module Jekyll::Spaceship
end end
end end
def self.exclude(*types)
@@_exclusions = types
end
def html?(_ext) def html?(_ext)
self.class.html? _ext self.class.html? _ext
end end
@ -217,6 +233,34 @@ module Jekyll::Spaceship
html?(output_ext) or markdown?(ext) html?(output_ext) or markdown?(ext)
end end
def pre_exclude(content)
@_exclusion_store = []
@_exclusions.each do |type|
regex = nil
if type == :code
regex = /(`{3}\s*(\w*)((?:.|\n)*?)`{3})/
end
next if regex.nil?
content.scan(regex) do |match_data|
match = match_data[0]
id = @_exclusion_store.size
content = content.gsub(match, "[//]: JEKYLL_EXCLUDE_##{id}")
@_exclusion_store.push match
end
end
content
end
def after_exclude(content)
while @_exclusion_store.size > 0
match = @_exclusion_store.pop
id = @_exclusion_store.size
content = content.gsub("[//]: JEKYLL_EXCLUDE_##{id}", match)
end
@_exclusion_store = []
content
end
def on_handled def on_handled
processor = self.class.name.split('::').last processor = self.class.name.split('::').last
file = page.path.gsub(/.*_posts\//, '') file = page.path.gsub(/.*_posts\//, '')
@ -251,7 +295,9 @@ module Jekyll::Spaceship
# dispatch to other handlers # dispatch to other handlers
if processor.respond_to? method if processor.respond_to? method
blk_content = processor.pre_exclude blk_content
blk_content = processor.send method, blk_content blk_content = processor.send method, blk_content
blk_content = processor.after_exclude blk_content
end end
end end

View File

@ -2,6 +2,8 @@
module Jekyll::Spaceship module Jekyll::Spaceship
class PlantUMLProcessor < Processor class PlantUMLProcessor < Processor
exclude :none
def on_handle_markdown(content) def on_handle_markdown(content)
# match default plantuml block and code block # match default plantuml block and code block
pattern = Regexp.union( pattern = Regexp.union(