|
| 1 | +module Idio |
| 2 | + |
| 3 | + class Helper |
| 4 | + attr_accessor :language, :separator, :content, :fence |
| 5 | + |
| 6 | + def initialize(context, path) |
| 7 | + |
| 8 | + @config= context.registers[:site].config.fetch("idio",{}) |
| 9 | + |
| 10 | + prefix = @config.fetch("path", nil) |
| 11 | + here = File.dirname(context.registers[:page]["path"]) |
| 12 | + |
| 13 | + if not path.nil? |
| 14 | + if not prefix.nil? |
| 15 | + @path = File.join(here, prefix, path) |
| 16 | + else |
| 17 | + @path = File.join(here, path) |
| 18 | + end |
| 19 | + else |
| 20 | + if not prefix.nil? |
| 21 | + @path = File.join(here, prefix) |
| 22 | + else |
| 23 | + raise SyntaxError.new("No idio file supplied") |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + begin |
| 28 | + @content = File.read(@path) |
| 29 | + rescue Errno::ENOENT |
| 30 | + raise SyntaxError.new("Missing idio file #{@path}") |
| 31 | + end |
| 32 | + |
| 33 | + extension = File.extname(@path).sub('.','') |
| 34 | + |
| 35 | + case extension |
| 36 | + when "py" |
| 37 | + @language = "python" |
| 38 | + @separator = "###" |
| 39 | + when "rb" |
| 40 | + @language = "ruby" |
| 41 | + @separator = "###" |
| 42 | + when "cpp", 'h', 'hpp', 'cc', 'c', 'cu' |
| 43 | + @language = "cpp" |
| 44 | + @separator = "///" |
| 45 | + when "java" |
| 46 | + @language = "java" |
| 47 | + @separator = "///" |
| 48 | + when "js" |
| 49 | + @language = "javascript" |
| 50 | + @separator = "///" |
| 51 | + else |
| 52 | + @separator = "###" |
| 53 | + @language = "" |
| 54 | + end |
| 55 | + |
| 56 | + @fence = @config.fetch("fence", false) |
| 57 | + |
| 58 | + end |
| 59 | + |
| 60 | + def section(name) |
| 61 | + |
| 62 | + file_section_expression = /^\s*#{@separator}\s*(.*)\s*$/ |
| 63 | + |
| 64 | + raw_sections = @content.split(file_section_expression) |
| 65 | + |
| 66 | + raw_sections = ["Beginning"]+raw_sections |
| 67 | + |
| 68 | + sections= raw_sections.each_slice(2).map{ |k, v| |
| 69 | + [k.downcase.gsub('"','') , v] }.to_h |
| 70 | + |
| 71 | + if not sections.keys.include?(name) |
| 72 | + raise SyntaxError.new("No such idio section as #{name} in #{@path}. Available: #{sections.keys} ") |
| 73 | + end |
| 74 | + |
| 75 | + return sections[name] |
| 76 | + |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + class Context < Liquid::Block |
| 81 | + def initialize(tag_name, markup, tokens) |
| 82 | + super |
| 83 | + @path = markup.strip.gsub('"','') |
| 84 | + end |
| 85 | + |
| 86 | + def render(context) |
| 87 | + config= context.registers[:site].config.fetch("idio",{}) |
| 88 | + @old = config['path'] |
| 89 | + @old = "" if @old.nil? |
| 90 | + config['path']=File.join(@old, @path) |
| 91 | + context.registers[:site].config["idio"]=config |
| 92 | + catch = super |
| 93 | + config['path']=@old |
| 94 | + context.registers[:site].config["idio"]=config |
| 95 | + return catch |
| 96 | + end |
| 97 | + |
| 98 | + end |
| 99 | + |
| 100 | + class FragmentTag < Liquid::Tag |
| 101 | + Syntax = /([^,]*)(?:\s*,\s*(.*))?/o |
| 102 | + |
| 103 | + def initialize(tag_name, markup, tokens) |
| 104 | + super |
| 105 | + |
| 106 | + if markup =~ Syntax |
| 107 | + if $2 |
| 108 | + @path = $2.strip |
| 109 | + else |
| 110 | + @path = nil |
| 111 | + end |
| 112 | + |
| 113 | + @label = $1.downcase.strip.gsub('"','') |
| 114 | + |
| 115 | + else |
| 116 | + raise SyntaxError.new("Syntax error in idio fragment parsing: #{markup}") |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + def render(context) |
| 121 | + |
| 122 | + helper = Helper.new(context, @path) |
| 123 | + |
| 124 | + content = helper.section(@label) |
| 125 | + |
| 126 | + if helper.fence |
| 127 | + return "``` #{helper.language}\n#{content}\n```\n" |
| 128 | + else |
| 129 | + return content |
| 130 | + end |
| 131 | + |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + class CodeTag < Liquid::Tag |
| 136 | + |
| 137 | + def initialize(tag_name, markup, tokens) |
| 138 | + super |
| 139 | + |
| 140 | + @path = markup.strip.gsub('"','') |
| 141 | + |
| 142 | + end |
| 143 | + |
| 144 | + def render(context) |
| 145 | + |
| 146 | + helper = Helper.new(context, @path) |
| 147 | + |
| 148 | + content = helper.content |
| 149 | + |
| 150 | + if helper.fence |
| 151 | + return "``` #{helper.language}\n#{content}\n```\n" |
| 152 | + else |
| 153 | + return content |
| 154 | + end |
| 155 | + |
| 156 | + end |
| 157 | + end |
| 158 | +end |
| 159 | + |
| 160 | +Liquid::Template.register_tag('idio', Idio::Context) |
| 161 | +Liquid::Template.register_tag('fragment', Idio::FragmentTag) |
| 162 | +Liquid::Template.register_tag('code', Idio::CodeTag) |
0 commit comments