Class: RedditPostToMarkdown::PostRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/reddit_post_to_markdown/post_renderer.rb

Overview

Converts Reddit post data and its comments into a Markdown string.

The output format matches the reddit-markdown tool: post header, title, selftext, reply count, and a depth-indented comment tree.

Constant Summary collapse

DEFAULT_FILTERED_MESSAGE =

Replacement text used when a comment matches a filter and no custom :message is provided in the filters hash.

"REMOVED DUE TO CUSTOM FILTER(S)"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(post_data, replies_data, filters = {}) ⇒ PostRenderer

Returns a new instance of PostRenderer.

Parameters:

  • post_data (Hash)

    Reddit post data hash (see render)

  • replies_data (Array<Hash>)

    top-level comment objects (see render)

  • filters (Hash) (defaults to: {})

    optional comment filters (see render)



34
35
36
37
38
# File 'lib/reddit_post_to_markdown/post_renderer.rb', line 34

def initialize(post_data, replies_data, filters = {})
  @post_data    = post_data
  @replies_data = replies_data
  @filters      = filters || {}
end

Class Method Details

.render(post_data, replies_data, filters: {}) ⇒ String

Renders a Reddit post and its comments as a Markdown string.

This is the primary entry point for the class. It instantiates a renderer and calls #render.

Parameters:

  • post_data (Hash)

    the data object from Reddit’s post listing JSON, containing keys such as “title”, “author”, “selftext”, “ups”, “locked”, “created_utc”, and “subreddit_name_prefixed”

  • replies_data (Array<Hash>)

    the children array from Reddit’s comment listing JSON; each element represents a top-level comment

  • filters (Hash) (defaults to: {})

    optional comment filters (see RedditPostToMarkdown.convert for full key documentation)

Returns:

  • (String)

    the fully rendered Markdown



27
28
29
# File 'lib/reddit_post_to_markdown/post_renderer.rb', line 27

def self.render(post_data, replies_data, filters: {})
  new(post_data, replies_data, filters).render
end

Instance Method Details

#renderString

Renders the post and all its comments as a single Markdown string.

Sections in order:

  1. Post header (subreddit, author, upvotes, timestamp)

  2. Post title as an H2

  3. Link back to the original post

  4. Lock notice (if the thread is locked)

  5. Post body / selftext as a block-quote (if present)

  6. Total reply count

  7. Horizontal rule

  8. Comment tree, depth-indented with tab characters

Returns:

  • (String)


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
86
87
88
# File 'lib/reddit_post_to_markdown/post_renderer.rb', line 53

def render
  lines = []

  # Post header
  lines << "#{header_line}"
  lines << "## #{post_title}"
  lines << "Original post: [#{post_url}](#{post_url})"
  lines << lock_message if post_locked?

  # Selftext
  if post_selftext && !post_selftext.strip.empty?
    decoded = decode_selftext(post_selftext)
    lines << "> #{decoded.gsub("\n", "\n> ")}"
  end

  image_urls = post_image_urls()
  if image_urls.size > 0
    lines << "### Images"
    image_urls.each do |url|
      lines << "![no alt text](#{url})"
    end
    lines << ""
  end

  # Reply count + separator
  total = count_all_replies
  lines << "💬 ~ #{total} replies"
  lines << "---\n"

  # Top-level comments
  @replies_data.each do |reply_obj|
    render_top_level_reply(reply_obj, lines)
  end

  lines.join("\n")
end