Rules 4.12 KB
Newer Older
1 2 3
# -*- mode: ruby -*-
# vi: set ft=ruby :

Evan Read's avatar
Evan Read committed
4
TITLE_FILTER_REGEXP = /(#\s|#{BadgesFilter::BADGES_MARKDOWN_PATTERN})/.freeze
Marcia Ramos's avatar
Marcia Ramos committed
5

6
preprocess do
Marcia Ramos's avatar
Marcia Ramos committed
7 8
  badges_filter = BadgesFilter.new

9 10
  @items.each do |item|
    if item.identifier.to_s.end_with?(".md") && !item.binary?
Marcia Ramos's avatar
Marcia Ramos committed
11 12
      # If there isn't already a 'redirect_to' defined in the yaml frontmatter,
      # use the text to assume the redirect URL.
13
      unless item[:redirect_to]
Marcia Ramos's avatar
Marcia Ramos committed
14 15 16
        if item.raw_content =~ /^This document was moved to \[.*\]\(.*\)/m
          # Capture the intended page so the redirect page can redirect to it.
          item[:redirect_to] = item.raw_content.match(/^This document was moved to \[.*\]\((.*)\)/m)[1]
17 18 19 20
          # Correct the URL.
          item[:redirect_to] = item[:redirect_to].gsub!(/\.md/, '.html')
        end
      else
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
21 22 23 24 25 26 27
        # If the provided path is in Markdown
        if item[:redirect_to].to_s.match(/.md/)
          # If it's not a full URL
          unless item[:redirect_to].to_s.match(/^http/)
            # Correct it to HTML
            item[:redirect_to] = item[:redirect_to].gsub!(/\.md/, '.html')
          end
Marcia Ramos's avatar
Marcia Ramos committed
28
        end
29
      end
30

Marcia Ramos's avatar
Marcia Ramos committed
31 32 33 34 35 36 37 38
      # We need to do some transformations for the title
      # Let's extract and keep it first:
      raw_title = item.raw_content.match(/^[#] .*$/).to_s

      # If we don't have a title in frontmatter, reuse the one we just fetch
      item[:title] ||= raw_title.gsub(TITLE_FILTER_REGEXP, '')

      # Run badges filter with title fragment
39
      item[:title_badge] = badges_filter.run_from_markdown(raw_title.match(BadgesFilter::BADGES_MARKDOWN_PATTERN).to_s)
40 41 42 43
    end
  end
end

44 45 46 47 48 49
compile '/404.*' do
  filter :erb
  layout '/404.*'
  write '/404.html'
end

50
compile '/archives/index.*' do
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
51 52 53 54 55
  filter :erb
  layout '/archives.*'
  write '/archives/index.html'
end

Connor Shea's avatar
Connor Shea committed
56 57 58 59 60
compile '/**/*.html' do
  layout '/default.*'
end

compile '/**/*.md' do
61
  if item[:redirect_to].nil?
62 63 64 65 66

    # If 'toc' is absent in a file's yaml frontmatter, show ToC.
    # Set to 'toc: false' to disable it.
    include_toc = item[:toc].nil? ? true : false

67 68
    # Use GitlabKramdown with Rouge.
    # https://gitlab.com/brodock/gitlab_kramdown
69
    filter :gitlab_kramdown,
Evan Read's avatar
Evan Read committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
           input: 'GitlabKramdown',
           syntax_highlighter: 'rouge',
           syntax_highlighter_opts: {
             # In kramdown 2.0, the plaintext parser was removed and replaced by the
             # :guess_lang option:
             #
             # - https://github.com/gettalong/kramdown/blob/master/doc/news/release_2_0_0.page
             # - https://github.com/gettalong/kramdown/pull/573
             guess_lang: true
           },
           default_lang: 'Plain Text',
           hard_wrap: false,
           auto_ids: true,
           toc_levels: 2..5,
           with_toc: include_toc
Marcia Ramos's avatar
Marcia Ramos committed
85

86 87
    filter :convert_mermaid_html

Marcia Ramos's avatar
Marcia Ramos committed
88
    filter :colorize_syntax,
Evan Read's avatar
Evan Read committed
89
           default_colorizer: :rouge
90

91 92 93
    # GitLab price / tiers specific badges
    filter :badges

94
    filter :md_to_html_ext
95
    filter :admonition
96
    filter :icons
97
    filter :introduced_in
98 99 100 101 102 103

    if item[:layout].nil?
      layout '/default.*'
    else
      layout "/#{item[:layout]}.*"
    end
104 105 106
  else
    layout '/redirect.*'
  end
Connor Shea's avatar
Connor Shea committed
107 108
end

109
compile '/**/*.scss' do
Connor Shea's avatar
Connor Shea committed
110
  filter :erb
111
  filter :sass,
Evan Read's avatar
Evan Read committed
112 113
         syntax: :scss,
         style: :compressed
Evan Read's avatar
Evan Read committed
114
  write "#{item.identifier.without_ext}-v#{rep.item[:version]}.css"
Connor Shea's avatar
Connor Shea committed
115 116
end

117 118 119
# Do not compile minified assets
passthrough '/**/*.min.*'

Jacques Erasmus's avatar
Jacques Erasmus committed
120
compile '/assets/javascripts/*.js' do
121
  filter :erb
Evan Read's avatar
Evan Read committed
122
  write "#{item.identifier.without_ext}-v#{rep.item[:version]}.js"
123 124
end

125 126
compile '/index.*' do
  filter :erb
127
  layout '/home.*'
128 129 130
  write '/index.html'
end

131 132 133 134 135 136 137 138 139 140
compile '/sitemap.*' do
  filter :erb
  write '/sitemap.xml'
end

compile '/robots.*' do
  filter :erb
  write '/robots.txt'
end

Connor Shea's avatar
Connor Shea committed
141 142 143 144
route '/**/*.{html,md}' do
  if item.identifier =~ '/index.*'
    '/index.html'
  else
Evan Read's avatar
Evan Read committed
145
    "#{item.identifier.without_ext}.html"
Connor Shea's avatar
Connor Shea committed
146 147 148
  end
end

149 150 151
# Leave yaml files out
ignore '/_data/*.yaml'

Connor Shea's avatar
Connor Shea committed
152 153 154 155 156 157
compile '/**/*' do
  unless item.identifier.ext == 'scss'
    write item.identifier.to_s
  end
end

158 159
layout '/**/*', :erb

Connor Shea's avatar
Connor Shea committed
160 161
# Leave the favicon alone.
passthrough '/favicon.ico'
Jacques Erasmus's avatar
Jacques Erasmus committed
162 163 164

postprocess do
  system('nanoc frontend')
165 166

  Gitlab::SymlinksConverter.run(config, items)
Jacques Erasmus's avatar
Jacques Erasmus committed
167
end