summaryrefslogtreecommitdiff
path: root/lib/gitlab/markup_helper.rb
blob: d419fa66e57f7f7b66499125dc8958df8ce18af1 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

module Gitlab
  module MarkupHelper
    extend self

    MARKDOWN_EXTENSIONS = %w[mdown mkd mkdn md markdown].freeze
    ASCIIDOC_EXTENSIONS = %w[adoc ad asciidoc].freeze
    OTHER_EXTENSIONS = %w[textile rdoc org creole wiki mediawiki rst].freeze
    EXTENSIONS = MARKDOWN_EXTENSIONS + ASCIIDOC_EXTENSIONS + OTHER_EXTENSIONS
    PLAIN_FILENAMES = %w[readme index].freeze

    # Public: Determines if a given filename is compatible with GitHub::Markup.
    #
    # filename - Filename string to check
    #
    # Returns boolean
    def markup?(filename)
      EXTENSIONS.include?(extension(filename))
    end

    # Public: Determines if a given filename is compatible with
    # GitLab-flavored Markdown.
    #
    # filename - Filename string to check
    #
    # Returns boolean
    def gitlab_markdown?(filename)
      MARKDOWN_EXTENSIONS.include?(extension(filename))
    end

    # Public: Determines if the given filename has AsciiDoc extension.
    #
    # filename - Filename string to check
    #
    # Returns boolean
    def asciidoc?(filename)
      ASCIIDOC_EXTENSIONS.include?(extension(filename))
    end

    # Public: Determines if the given filename is plain text.
    #
    # filename - Filename string to check
    #
    # Returns boolean
    def plain?(filename)
      extension(filename) == 'txt' || plain_filename?(filename)
    end

    def previewable?(filename)
      markup?(filename)
    end

    private

    def extension(filename)
      File.extname(filename).downcase.delete('.')
    end

    def plain_filename?(filename)
      PLAIN_FILENAMES.include?(filename.downcase)
    end
  end
end