Module: Dumpers

Defined in:
lib/dumpers.rb

Overview

Methods to display Jekyll variable contents

Class Method Summary collapse

Class Method Details

.attributes_as_string(object, attrs) ⇒ Object



166
167
168
# File 'lib/dumpers.rb', line 166

def attributes_as_string(object, attrs)
  attrs.map { |attr| "    #{attr.to_s.delete_prefix("@")} = #{object.instance_variable_get(attr)}" }
end

.collection_as_string(collection, indent_spaces) ⇒ Object

See github.com/jekyll/jekyll/blob/master/lib/jekyll/collection.rb

attr_reader :site, :label, :metadata
attr_writer :docs
 is a hash with at least these keys: output[Boolean], permalink[String]
selected methods: collection_dir, directory, entries, exists?, files, filtered_entries, relative_directory


8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dumpers.rb', line 8

def collection_as_string(collection, indent_spaces)
  indent = ' ' * indent_spaces
  result = "    '\#{collection.label}' collection within '\#{collection.relative_directory}' subdirectory\n      \#{indent}Directory: \#{collection.directory}\n      \#{indent}Does the directory exist and is it not a symlink if in safe mode? \#{collection.exists?}\n      \#{indent}Collection_dir: \#{collection.collection_dir}\n      \#{indent}Metadata: \#{collection.metadata}\n      \#{indent}Static files: \#{collection.files}\n      \#{indent}Filtered entries: \#{collection.filtered_entries}\n  END_COLLECTION\n  result.chomp\nend\n"

.count_lines(string) ⇒ Object



22
23
24
25
26
# File 'lib/dumpers.rb', line 22

def count_lines(string)
  return string.split("\n").length if string

  0
end

.dump_document(logger, msg, document) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dumpers.rb', line 43

def dump_document(logger, msg, document)
  attributes = attributes_as_string(document, [:@path, :@extname, :@type])
  data = document.data.map { |k, v| "    #{k} = #{safe_to_s(v)}" }
  logger.info do
    "      \#{msg}\n        document dated \#{document.date.to_date}:\n          relative_path: \#{document.relative_path}:\n      \#{attributes.join(\"\\n\")}\n          Is it a draft? \#{document.draft?}\n          collection = \#{collection_as_string(document.collection, 4)}\n          content not dumped because it would likely be too long\n          site not dumped also\n          data:\n        \#{data.join(\"\\n  \").rstrip.chomp}\n    END_DOC\n  end\nend\n"

.dump_page(logger, msg, page) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dumpers.rb', line 66

def dump_page(logger, msg, page)
  attributes = attributes_as_string(page, [:@basename, :@ext, :@name])
  # site = page.site available if you need it
  data = page.data.map { |k, v| "    #{k} = #{v}" }
  logger.info do
    "      \#{msg}\\n  page at \#{page.dir}:\n      \#{attributes.join(\"\\n\")}\n          Is it HTML? \#{page.html?}; is it an index? \#{page.index?}\n          Permalink: \#{page.permalink}\n          URL: \#{page.url}\n          content not dumped because it would likely be too long\n          site not dumped also\n          Excerpt: \"\#{page.excerpt}\"\n        data:\n      \#{data.join(\"\\n\")}\n    END_PAGE\n  end\nend\n"

.dump_payload(logger, msg, payload) ⇒ Object

payload.page is a Jekyll::Drops::DocumentDrop, which contains this payload, see github.com/jekyll/jekyll/blob/master/lib/jekyll/drops/document_drop.rb



92
93
94
95
96
97
98
99
100
101
# File 'lib/dumpers.rb', line 92

def dump_payload(logger, msg, payload)
  result = "    \#{msg} payload:\n      content contains \#{count_lines(payload.content)} lines.\#{first_5_lines(payload.content)}\n      layout = \#{payload.layout}\n      highlighter_prefix = \#{payload.highlighter_prefix}\n      paginator and site not dumped.\n  END_INFO\n  logger.info { result.chomp }\nend\n"

.dump_site(logger, msg, site) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dumpers.rb', line 120

def dump_site(logger, msg, site) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  logger.info do
    "      \#{msg} site\n      site is of type \#{site.class}\n      site.time = \#{site.time}\n    END_INFO\n  end\n  env = site.config['env']\n  if env\n    mode = env['JEKYLL_ENV']\n    logger.info { \"site.config['env']['JEKYLL_ENV'] = \#{mode}\" }\n  else\n    logger.info { \"site.config['env'] is undefined\" }\n  end\n  site.collections.each do |key, _|\n    logger.info { \"site.collections.\#{key}\" }\n  end\n\n  # key env contains all environment variables, quite verbose so output is reduced to just the \"env\" key\n  logger.info { \"site.config has \#{site.config.length} entries:\" }\n  site.config.sort.each { |key, value| logger.info { \"  site.config.\#{key} = '\#{value}'\" unless key == \"env\" } }\n\n  logger.info { \"site.data has \#{site.data.length} entries:\" }\n  site.data.sort.each { |key, value| logger.info { \"  site.data.\#{key} = '\#{value}'\" } }\n\n  logger.info { \"site.documents has \#{site.documents.length} entries.\" }\n  site.documents.each { |key, _value| logger.info \"site.documents.\#{key}\" }\n\n  logger.info do\n    <<~END_INFO\n      site.keep_files has \#{site.keep_files.length} entries.\n      site.keep_files: \#{site.keep_files.sort}\n      site.pages has \#{site.pages.length} entries.\n    END_INFO\n  end\n\n  site.pages.each { |key, _value| logger.info \"site.pages.\#{key}\" }\n\n  logger.info { \"site.posts has \#{site.posts.docs.length} entries.\" }\n  site.posts.docs.each { |key, _value| logger.info \"site.posts.docs.\#{key}\" }\n\n  logger.info { \"site.tags has \#{site.tags.length} entries.\" }\n  site.tags.sort.each { |key, value| logger.info { \"site.tags.\#{key} = '\#{value}'\" } }\nend\n"

.first_5_lines(string) ⇒ Object



103
104
105
106
107
108
# File 'lib/dumpers.rb', line 103

def first_5_lines(string)
  lines = string ? string.split("\n")[0..4] : []
  return "\n    first 5 lines:\n    #{lines.join("\n    ")}\n" if lines.length.positive?

  ""
end

.safe_to_s(value) ⇒ Object

Calling value.to_s blows up when a Jekyll::Excerpt Error message is unrelated to the problem, makes it hard to track down Be careful when converting values to string!



31
32
33
34
35
36
37
# File 'lib/dumpers.rb', line 31

def safe_to_s(value)
  return value.content if value.is_a? Jekyll::Excerpt

  value.to_s
rescue StandardError => e
  e.message
end