|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'English' |
| 4 | + |
| 5 | +module Jekyll |
| 6 | + # Definition of the filters. |
| 7 | + module CodeHighlightFilters |
| 8 | + def manual_highlight_code(input) |
| 9 | + new_input = input |
| 10 | + last_code_block_end = 0 |
| 11 | + until (code_block = find_next_code_block(new_input, last_code_block_end)).nil? |
| 12 | + unless code_block.valid? |
| 13 | + last_code_block_end = code_block.end |
| 14 | + next |
| 15 | + end |
| 16 | + |
| 17 | + code_block_begin = '' |
| 18 | + code_block_begin = new_input[0..code_block.begin - 1] || '' if code_block.begin > 0 |
| 19 | + new_input = code_block_begin \ |
| 20 | + + replace_highlights(code_block.contents, block: code_block.block?) \ |
| 21 | + + (new_input[code_block.end..] || '') |
| 22 | + |
| 23 | + last_code_block_end = 0 |
| 24 | + end |
| 25 | + |
| 26 | + new_input |
| 27 | + end |
| 28 | + |
| 29 | + private |
| 30 | + |
| 31 | + CodeBlock = Struct.new(:begin, :end, :delimiter, :data) do |
| 32 | + def valid? |
| 33 | + !(delimiter == '`' && !block?) || (block? && %w[manual manual-highlight].include?(type)) |
| 34 | + end |
| 35 | + |
| 36 | + def block? |
| 37 | + data.include?("\n") |
| 38 | + end |
| 39 | + |
| 40 | + def contents |
| 41 | + if block? |
| 42 | + new_content = data.delete_prefix("#{type}\n") |
| 43 | + new_content = unindent(new_content) |
| 44 | + |
| 45 | + # Make sure that it ends with a newline. |
| 46 | + new_content = "#{new_content.delete_suffix("\n")}\n" |
| 47 | + return new_content |
| 48 | + end |
| 49 | + |
| 50 | + data |
| 51 | + end |
| 52 | + |
| 53 | + def type |
| 54 | + return '' unless block? |
| 55 | + |
| 56 | + /^(.+?)\n/ =~ data |
| 57 | + Regexp.last_match(1) || '' |
| 58 | + end |
| 59 | + |
| 60 | + private |
| 61 | + |
| 62 | + def unindent(str) |
| 63 | + loop do |
| 64 | + populated_lines_regex = /^.+/ |
| 65 | + unindent_regex = /^#{str.scan(/^[^\S\n\r]+/).min}/ |
| 66 | + |
| 67 | + # We hit the maximum. |
| 68 | + break unless str.scan(unindent_regex).length == str.scan(populated_lines_regex).length |
| 69 | + |
| 70 | + unindented_str = str.gsub(unindent_regex, '') |
| 71 | + str = unindented_str |
| 72 | + end |
| 73 | + |
| 74 | + str |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def find_next_code_block(input, from, delimiter = '`') |
| 79 | + code_block_start_regex = /(?:#{delimiter})+/ |
| 80 | + from_substr_start = input[from..] |
| 81 | + code_block_start_match = code_block_start_regex.match(from_substr_start) |
| 82 | + return nil if code_block_start_match.nil? |
| 83 | + |
| 84 | + code_block_start_begin = from + code_block_start_match.begin(0) |
| 85 | + code_block_start_end = from + code_block_start_match.end(0) |
| 86 | + code_block_delimiter = code_block_start_match[0] |
| 87 | + |
| 88 | + code_block_end_regex = /(?<!`)#{code_block_delimiter}(?!`)/ |
| 89 | + from_substr_end = input[code_block_start_end..] |
| 90 | + code_block_end_match = code_block_end_regex.match(from_substr_end) |
| 91 | + return nil if code_block_end_match.nil? |
| 92 | + |
| 93 | + code_block_end_begin = code_block_start_end + code_block_end_match.begin(0) |
| 94 | + code_block_end_end = code_block_start_end + code_block_end_match.end(0) |
| 95 | + |
| 96 | + code_block_data = input[code_block_start_end..code_block_end_begin - 1] |
| 97 | + |
| 98 | + CodeBlock.new(begin: code_block_start_begin, end: code_block_end_end, delimiter: code_block_delimiter, |
| 99 | + data: code_block_data) |
| 100 | + end |
| 101 | + |
| 102 | + def replace_highlights(input, block: false) |
| 103 | + return input if input.empty? |
| 104 | + |
| 105 | + highlights_regex = /(?<!\\)@\[(?<content>.*?)\]\((?<type>.+?)\)/ |
| 106 | + non_highlights_regex = /(?<non_highlight>.*?)(?:(?<highlight>#{highlights_regex})|$)/ |
| 107 | + |
| 108 | + num_highlights = input.scan(highlights_regex).length |
| 109 | + highlight_only = input.gsub(highlights_regex, '') == '' |
| 110 | + |
| 111 | + force_spaces_input = input.gsub(non_highlights_regex) do |_match| |
| 112 | + non_highlight = $LAST_MATCH_INFO[:non_highlight] |
| 113 | + highlight = $LAST_MATCH_INFO[:highlight] || '' |
| 114 | + non_highlight.gsub!('<', '<') |
| 115 | + non_highlight.gsub!('>', '>') |
| 116 | + non_highlight + highlight |
| 117 | + end |
| 118 | + |
| 119 | + highlights_result = force_spaces_input.gsub(highlights_regex) do |_match| |
| 120 | + type = $LAST_MATCH_INFO[:type] |
| 121 | + next '<wbr>' if type == 'wbr' |
| 122 | + |
| 123 | + classes = [] |
| 124 | + classes.push('code-highlight') if num_highlights == 1 && !block |
| 125 | + classes.push(type) |
| 126 | + content = $LAST_MATCH_INFO[:content] |
| 127 | + content.gsub!('<', '<') |
| 128 | + content.gsub!('>', '>') |
| 129 | + |
| 130 | + "<span class=\"#{classes.join(' ')}\">#{content}</span>" |
| 131 | + end |
| 132 | + |
| 133 | + if block |
| 134 | + code_tag = "<code class=\"highlight\">#{highlights_result}</code>" |
| 135 | + pre_tag = "<pre class=\"manual\" markdown=0>#{code_tag}</pre>" |
| 136 | + return pre_tag |
| 137 | + end |
| 138 | + |
| 139 | + return "<span class=\"code-highlight\">#{highlights_result}</span>" unless highlight_only && num_highlights == 1 |
| 140 | + |
| 141 | + highlights_result |
| 142 | + end |
| 143 | + end |
| 144 | +end |
| 145 | + |
| 146 | +Liquid::Template.register_filter(Jekyll::CodeHighlightFilters) |
0 commit comments