summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2016-02-21 19:22:02 -0500
committerRobert Speicher <rspeicher@gmail.com>2016-03-04 18:05:48 -0500
commit8eaeda081615346e1d428e9f4f4402d3bb24b9f1 (patch)
treedbd6e45e2f3951c32760e6ad5d11076864d16e11 /lib
parent74751791a8bf27c5576832f73a57a5b110b423ad (diff)
downloadgitlab-ce-8eaeda081615346e1d428e9f4f4402d3bb24b9f1.tar.gz
Add YamlFrontMatterFilter to the PreProcessPipeline
This filter will detect YAML Front Matter and convert it to an HTML table for prettier formatting.
Diffstat (limited to 'lib')
-rw-r--r--lib/banzai/filter/yaml_front_matter_filter.rb28
-rw-r--r--lib/banzai/pipeline/pre_process_pipeline.rb3
2 files changed, 30 insertions, 1 deletions
diff --git a/lib/banzai/filter/yaml_front_matter_filter.rb b/lib/banzai/filter/yaml_front_matter_filter.rb
new file mode 100644
index 00000000000..e4e2f3f228d
--- /dev/null
+++ b/lib/banzai/filter/yaml_front_matter_filter.rb
@@ -0,0 +1,28 @@
+require 'html/pipeline/filter'
+require 'yaml'
+
+module Banzai
+ module Filter
+ class YamlFrontMatterFilter < HTML::Pipeline::Filter
+ DELIM = '---'.freeze
+
+ # Hat-tip to Middleman: https://git.io/v2e0z
+ PATTERN = %r{
+ \A(?:[^\r\n]*coding:[^\r\n]*\r?\n)?
+ (?<start>#{DELIM})[ ]*\r?\n
+ (?<frontmatter>.*?)[ ]*\r?\n?
+ ^(?<stop>#{DELIM})[ ]*\r?\n?
+ \r?\n?
+ (?<content>.*)
+ }mx.freeze
+
+ def call
+ match = PATTERN.match(html)
+
+ return html unless match
+
+ "```yaml\n#{match['frontmatter']}\n```\n\n#{match['content']}"
+ end
+ end
+ end
+end
diff --git a/lib/banzai/pipeline/pre_process_pipeline.rb b/lib/banzai/pipeline/pre_process_pipeline.rb
index c174f0b862d..50dc978b452 100644
--- a/lib/banzai/pipeline/pre_process_pipeline.rb
+++ b/lib/banzai/pipeline/pre_process_pipeline.rb
@@ -2,7 +2,8 @@ module Banzai
module Pipeline
class PreProcessPipeline < BasePipeline
def self.filters
- [
+ FilterArray[
+ Filter::YamlFrontMatterFilter
]
end