Is it possible to parse and manipulate Tinderbox's XML using Ruby?

Salvete,

I found an interesting example. Inspired by this script for R in Zettelkasten compatibility with Markdown apps - #5 by jpl), I managed to extract the main attributes from the notes. I still could not, however, get some of them, such as $Container and $Path.

#!/usr/bin/env ruby
# frozen_string_literal: false

# bcdav 2021-12-11-23-36

Encoding.default_external = Encoding::UTF_8

require 'nokogiri'
require 'terminal-table'

doc = Nokogiri::XML(File.open('path/to/file'))

links = doc.xpath('//link')
links_rows = []

parse_links = true

if parse_links == true
  links.each do |node|
    links_rows << [node.attr('name'), node.attr('sourceid'), node.attr('destid'), node.attr('sstart'), node.attr('slen')]
  end

  headings = %w[name sourceid destid sstart slen]
  links_table = Terminal::Table.new rows: links_rows, headings: headings

  puts links_table

end

# parse_notes = false
parse_notes = true
elems = doc.xpath('//tinderbox//item')
notes_rows = []

if parse_notes == true

  elems.each do |node|
    id = node.attr('ID') || ''
    name = node.at('.//attribute[@name="Name"]').text || ''
    nl_tags = node.at('.//attribute[@name="NLTags"]') ? node.at('.//attribute[@name="NLTags"]').text : ''
    my_string = node.at('.//attribute[@name="MyString"]') ? node.at('.//attribute[@name="MyString"]').text : ''
    text = node.at('//text') ? node.at('//text').text : ''
    prototype = node.attr('proto') || ''
    # p path = node.css_path

    notes_rows << [id, name, nl_tags, my_string, text, prototype]
  end

  puts notes_rows.join(';')

end

Hopefully, this will be useful to someone at some point. Not that I have not seen many Ruby enthusiasts here, though.

2 Likes