Class: WeeklySnippets::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/weekly_snippets/publisher.rb

Instance Method Summary collapse

Constructor Details

#initialize(headline:, public_mode:, markdown_snippet_munger: nil) ⇒ Publisher

Returns a new instance of Publisher.

Parameters:

  • headline (String)

    Markdown string used to markup each section title within a block of snippet text

  • public_mode (true, false, or nil)

    indicates whether or not the snippets are to be published publicly; private snippets and redacted spans of text will not be published when public_mode is true

  • markdown_snippet_munger (Proc) (defaults to: nil)

    the code block that will be called with a String of snippet text after redaction and before Markdown preparation, to modify the snippet text String in-place; will not be called if the snippet’s version doesn’t support Markdown syntax



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.

Parameters:

  • text (String)

    snippet text

Returns:

  • (String)


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.

Parameters:

  • snippets (Hash<String,Hash>)

    timestamp => batch of snippets

Returns:

  • (Hash<String,Hash>)


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 |timestamp, snippet_batch|
    published = []
    snippet_batch.each do |snippet|
      unless @public_mode and !snippet['public']
        publish_snippet(snippet, published)
      end
    end
    result[timestamp] = 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.

Parameters:

  • snippet (Hash<String,String>)

    snippet hash with two fields: last-week and this-week

  • published (Array<Hash<String,String>>)

    array of published snippets



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