summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/jekyll-4.4.1/lib/jekyll/readers/post_reader.rb
blob: 25c5f98fb0700e13de922da439e31bcc3a138c65 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

module Jekyll
  class PostReader
    attr_reader :site, :unfiltered_content

    def initialize(site)
      @site = site
    end

    # Read all the files in <source>/<dir>/_drafts and create a new
    # Document object with each one.
    #
    # dir - The String relative path of the directory to read.
    #
    # Returns nothing.
    def read_drafts(dir)
      read_publishable(dir, "_drafts", Document::DATELESS_FILENAME_MATCHER)
    end

    # Read all the files in <source>/<dir>/_posts and create a new Document
    # object with each one.
    #
    # dir - The String relative path of the directory to read.
    #
    # Returns nothing.
    def read_posts(dir)
      read_publishable(dir, "_posts", Document::DATE_FILENAME_MATCHER)
    end

    # Read all the files in <source>/<dir>/<magic_dir> and create a new
    # Document object with each one insofar as it matches the regexp matcher.
    #
    # dir - The String relative path of the directory to read.
    #
    # Returns nothing.
    def read_publishable(dir, magic_dir, matcher)
      read_content(dir, magic_dir, matcher)
        .tap { |docs| docs.each(&:read) }
        .select { |doc| processable?(doc) }
    end

    # Read all the content files from <source>/<dir>/magic_dir
    #   and return them with the type klass.
    #
    # dir - The String relative path of the directory to read.
    # magic_dir - The String relative directory to <dir>,
    #   looks for content here.
    # klass - The return type of the content.
    #
    # Returns klass type of content files
    def read_content(dir, magic_dir, matcher)
      @site.reader.get_entries(dir, magic_dir).map do |entry|
        next unless matcher.match?(entry)

        path = @site.in_source_dir(File.join(dir, magic_dir, entry))
        Document.new(path,
                     :site       => @site,
                     :collection => @site.posts)
      end.tap(&:compact!)
    end

    private

    def processable?(doc)
      if doc.content.nil?
        Jekyll.logger.debug "Skipping:", "Content in #{doc.relative_path} is nil"
        false
      elsif !doc.content.valid_encoding?
        Jekyll.logger.debug "Skipping:", "#{doc.relative_path} is not valid UTF-8"
        false
      else
        publishable?(doc)
      end
    end

    def publishable?(doc)
      site.publisher.publish?(doc).tap do |will_publish|
        if !will_publish && site.publisher.hidden_in_the_future?(doc)
          Jekyll.logger.warn "Skipping:", "#{doc.relative_path} has a future date"
        end
      end
    end
  end
end