markup_helper.rb 1.62 KB
Newer Older
1 2
# frozen_string_literal: true

3
module Gitlab
4
  module MarkupHelper
5 6
    extend self

7 8 9
    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
10
    EXTENSIONS = MARKDOWN_EXTENSIONS + ASCIIDOC_EXTENSIONS + OTHER_EXTENSIONS
11
    PLAIN_FILENAMES = %w[readme index].freeze
12 13 14 15 16 17 18

    # Public: Determines if a given filename is compatible with GitHub::Markup.
    #
    # filename - Filename string to check
    #
    # Returns boolean
    def markup?(filename)
19
      EXTENSIONS.include?(extension(filename))
20 21 22 23 24 25 26 27 28
    end

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

32 33 34 35 36 37
    # Public: Determines if the given filename has AsciiDoc extension.
    #
    # filename - Filename string to check
    #
    # Returns boolean
    def asciidoc?(filename)
38
      ASCIIDOC_EXTENSIONS.include?(extension(filename))
39 40
    end

41 42 43 44 45 46
    # Public: Determines if the given filename is plain text.
    #
    # filename - Filename string to check
    #
    # Returns boolean
    def plain?(filename)
47
      extension(filename) == 'txt' || plain_filename?(filename)
48 49
    end

50
    def previewable?(filename)
51
      markup?(filename)
52
    end
53 54 55 56 57 58

    private

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

    def plain_filename?(filename)
      PLAIN_FILENAMES.include?(filename.downcase)
62
    end
63 64
  end
end