Class: PinboardLogseq::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/pinboard_logseq/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Cli

Returns a new instance of Cli.



17
18
19
# File 'lib/pinboard_logseq/cli.rb', line 17

def initialize(args)
  @args = args
end

Class Method Details

.execute(args) ⇒ Object



13
14
15
# File 'lib/pinboard_logseq/cli.rb', line 13

def self.execute(args)
  new(args).execute
end

Instance Method Details

#executeObject



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
# File 'lib/pinboard_logseq/cli.rb', line 21

def execute
  data = JSON.parse(File.read(IMPORT_FILE))

  pins = data.map { |json| Pin.from_hash(json) }

  pins_by_year_and_month = pins.group_by { |p| [p.time.year, p.time.month] }

  pins_by_year_and_month.each do |(year, month), pins|
    content = ""
    pins.each do |pin|
      year = pin.time.strftime("%Y")
      month_name_short = pin.time.strftime("%b")
      day = pin.time.day.ordinalize
      formatted_date = "#{month_name_short} #{day}, #{year}"
      content +=
        "- 🔖 [#{pin.description || "No description"}](#{pin.href}) [[#{formatted_date}]] "
      content += "\n> #{pin.extended}" if pin.extended && pin.extended != "" && pin.extended != "undefined"
      tag_bits = pin.tags.map { |t| "[[#{t}]]" }
      tag_bits << "[[Pinboard - To Read]]" if pin.to_read
      content += tag_bits.join(" ")
      content += "\n"
    end
    month_name_long = pins.first.time.strftime("%B")
    File.write(
      "output/Pinboard Import ___ #{year} ___ #{month_name_long}.md",
      content
    )
  end
end