Welcome to mirror list, hosted at ThFree Co, Russian Federation.

front_matter.rb « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d215a77f4d77add879a0ac74dbe1ca0bb8cea2fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

module Gitlab
  module FrontMatter
    DELIM_LANG = {
      '---' => 'yaml',
      '+++' => 'toml',
      ';;;' => 'json'
    }.freeze

    DELIM_UNTRUSTED = "(?:#{Gitlab::FrontMatter::DELIM_LANG.keys.map { |x| RE2::Regexp.escape(x) }.join('|')})".freeze

    # Original pattern:
    #   \A(?<encoding>[^\r\n]*coding:[^\r\n]*\R)? # optional encoding line
    #   (?<before>\s*)
    #   ^(?<delim>#{DELIM})[ \t]*(?<lang>\S*)\R # opening front matter marker (optional language specifier)
    #   (?<front_matter>.*?)                    # front matter block content (not greedy)
    #   ^(\k<delim> | \.{3})                    # closing front matter marker
    #   [^\S\r\n]*(\R|\z)
    # rubocop:disable Style/StringConcatenation
    # rubocop:disable Style/LineEndConcatenation
    PATTERN_UNTRUSTED =
      # optional encoding line
      "\\A(?P<encoding>[^\\r\\n]*coding:[^\\r\\n]*#{::Gitlab::UntrustedRegexp::BACKSLASH_R})?" +
      '(?P<before>\s*)' +

      # opening front matter marker (optional language specifier)
      "^(?P<delim>#{DELIM_UNTRUSTED})[ \\t]*(?P<lang>\\S*)#{::Gitlab::UntrustedRegexp::BACKSLASH_R}" +

      # front matter block content (not greedy)
      '(?P<front_matter>(?:\n|.)*?)' +

      # closing front matter marker
      "^((?P<delim_closing>#{DELIM_UNTRUSTED})|\\.{3})" +
      "[^\\S\\r\\n]*(#{::Gitlab::UntrustedRegexp::BACKSLASH_R}|\\z)"
    # rubocop:enable Style/LineEndConcatenation
    # rubocop:enable Style/StringConcatenation

    PATTERN_UNTRUSTED_REGEX =
      Gitlab::UntrustedRegexp.new(PATTERN_UNTRUSTED, multiline: true).freeze
  end
end