Skip to content

Commit 83d87f9

Browse files
committed
added option to force syntax highlighting language, example: {% include_code file lang:ruby %}. Fixes #108
1 parent 67211ed commit 83d87f9

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

plugins/code_block.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ class CodeBlock < Liquid::Block
5353
def initialize(tag_name, markup, tokens)
5454
@title = nil
5555
@caption = nil
56+
@filetype = nil
5657
@highlight = true
58+
if markup =~ /\s+lang:(\w+)/i
59+
@filetype = $1
60+
markup = markup.sub(/\s+lang:\w+\s*/i,'')
61+
end
5762
if markup =~ CaptionUrlTitle
5863
@file = $1
5964
@caption = "<figcaption><span>#{$1}</span><a href='#{$2 + $3}'>#{$4}</a></figcaption>"
@@ -64,7 +69,7 @@ def initialize(tag_name, markup, tokens)
6469
@file = $1
6570
@caption = "<figcaption><span>#{$1}</span></figcaption>\n"
6671
end
67-
if @file =~ /\S[\S\s]*\w+\.(\w+)/
72+
if @file =~ /\S[\S\s]*\w+\.(\w+)/ && @filetype.nil?
6873
@filetype = $1
6974
end
7075
super

plugins/include_code.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class IncludeCodeTag < Liquid::Tag
3030
def initialize(tag_name, markup, tokens)
3131
@title = nil
3232
@file = nil
33+
if markup.strip =~ /\s+lang:(\w+)/i
34+
@filetype = $1
35+
markup = markup.strip.sub(/\s+lang:\w+\s*/i,'')
36+
end
3337
if markup.strip =~ /(.*)?(\s+|^)(\/*\S+)/i
3438
@title = $1 || nil
3539
@file = $3
@@ -52,7 +56,7 @@ def render(context)
5256

5357
Dir.chdir(code_path) do
5458
code = file.read
55-
@filetype = file.extname.sub('.','')
59+
@filetype = file.extname.sub('.','') if @filetype.nil?
5660
title = @title ? "#{@title} (#{file.basename})" : file.basename
5761
url = "#{context.registers[:site].config['url']}/#{code_dir}/#{@file}"
5862
source = "<div><figure role=code><figcaption><span>#{title}</span> <a href='#{url}'>download</a></figcaption>\n"

0 commit comments

Comments
 (0)