Class: WeeklySnippets::Publisher
- Inherits:
-
Object
- Object
- WeeklySnippets::Publisher
- Defined in:
- lib/weekly_snippets/publisher.rb
Instance Method Summary collapse
-
#initialize(headline:, public_mode:, markdown_snippet_munger: nil) ⇒ Publisher
constructor
A new instance of Publisher.
-
#prepare_markdown(text) ⇒ String
Processes snippet text in Markdown format to smooth out any anomalies before rendering.
-
#publish(snippets) ⇒ Hash<String,Hash>
Processes
snippets
entries for publication. -
#publish_snippet(snippet, published) ⇒ Object
Parses and publishes a snippet.
-
#redact!(text) ⇒ Object
Parses “and “}” redaction markers.
Constructor Details
#initialize(headline:, public_mode:, markdown_snippet_munger: nil) ⇒ Publisher
Returns a new instance of Publisher.
28 29 30 31 32 |
# File 'lib/weekly_snippets/publisher.rb', line 28 def initialize(headline:, public_mode:, markdown_snippet_munger:nil) @headline = headline @public_mode = public_mode @markdown_snippet_munger = markdown_snippet_munger end |
Instance Method Details
#prepare_markdown(text) ⇒ String
Processes snippet text in Markdown format to smooth out any anomalies before rendering. Also translates arbitrary plaintext to Markdown.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/weekly_snippets/publisher.rb', line 90 def prepare_markdown(text) parsed = [] uses_item_markers = (text =~ /^[-*]/) text.each_line do |line| line.rstrip! # Convert headline markers. line.sub!(/^(#+)/, @headline) # Add item markers for those who used plaintext and didn't add them; # add headline markers for those who defined different sections and # didn't add them. if line =~ /^([A-Za-z0-9])/ line = uses_item_markers ? "#{@headline} #{line}" : "- #{line}" end # Fixup item markers missing a space. line.sub!(/^[-*]([^ ])/, '- \1') parsed << line unless line.empty? end parsed.join("\n") end |
#publish(snippets) ⇒ Hash<String,Hash>
Processes snippets
entries for publication. Any snippets that should not appear when in public_mode
are removed from snippets
. The keys of the resulting hash will be sorted in nondecreasing order.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/weekly_snippets/publisher.rb', line 39 def publish(snippets) result = {} snippets.each do |, snippet_batch| published = [] snippet_batch.each do |snippet| unless @public_mode and !snippet['public'] publish_snippet(snippet, published) end end result[] = published unless published.empty? end result.sort.to_h end |
#publish_snippet(snippet, published) ⇒ Object
Parses and publishes a snippet. Filters out snippets rendered empty after redaction.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/weekly_snippets/publisher.rb', line 58 def publish_snippet(snippet, published) ['last-week', 'this-week'].each do |field| text = snippet[field] || '' redact! text if snippet['markdown'] @markdown_snippet_munger.yield text if @markdown_snippet_munger text = prepare_markdown text end snippet[field] = text.empty? ? nil : text end is_empty = (snippet['last-week'] || '').empty? && ( snippet['this-week'] || '').empty? published << snippet unless is_empty end |
#redact!(text) ⇒ Object
Parses “and “}” redaction markers. For public snippets, will redact everything between each set of markers. For internal snippets, will only remove the markers.
77 78 79 80 81 82 83 |
# File 'lib/weekly_snippets/publisher.rb', line 77 def redact!(text) if @public_mode text.gsub!(/\n?\{\{.*?\}\}/m,'') else text.gsub!(/(\{\{|\}\})/,'') end end |