Sorry man. I know the feeling. Let me see if I can help you out. This may only take you 5-10 minutes but it will simplify your life. You will only have to run the script once and get what you need.
-
I am assuming you have already installed pandoc and ruby, and downloaded the TaskPaper-Ruby library. Now install this gem for ruby like this,
gem install pandoc-ruby
-
Inside your TaskPaper-Ruby library, create a file INSIDE the
src
directory with the nametaskpaper+mmd.rb
-
Include this source in that file,
#!/usr/bin/ruby
# TaskPaperRuby
#
# This extends Matt's library to produce Markdown
#
# Made by Victor Gutierrez
require_relative 'taskpaperdocument'
class TaskPaperDocument
def to_mmd(only_type = nil)
@root_item ? @root_item.to_mmd(only_type) : ''
end
end
require_relative 'taskpaperitem'
class TaskPaperItem
def to_mmd(only_type = nil)
output = ''
if @type != TYPE_NULL
posn = 0
# This is pretty self-explanatory. It doesn't work perfectly with the projects since it leaves an empty space
remove_all_tags
if @type == TYPE_TASK
output += @content.to_s
# This removes the task identifier which happens to be `-`. In order to do that, we remove the first
# two characters in the string
# output += (@content[2..-1]).to_s
end
if @type == TYPE_NOTE
# Here the notes will be italiziced.
output += "*#{@content}*"
end
if @type == TYPE_PROJECT
# First, add an extra line before a project begins to create a better separation.
output += TaskPaperItem.linebreak.to_s
# This right here will add the right heading level according to where the project is in TaskPaper
$i = -1
begin
output += '#'
$i += 1
end while $i < effective_level
# Since we don't want the project marker from taskpaper which is `:` at the end of the string, we remove it
output += " #{@content[posn..-2]}"
end
# Add a space after every item.
output += TaskPaperItem.linebreak.to_s
end
@children.each do |child|
output += child.to_mmd(only_type)
end
output = output.to_s
end
end
The source is kind of self explanatory. I documented a couple of changes that you might want to make. I also made it easy to see the changes. So that for example, in the TYPE_NOTE, the text is italicized using Markdown. If you prefer to do bold or something, just change that using markdown.
Right now I also made it so that it removes all tags, but you can change that by removing the remove_all_tags
line. I would recommend simply to document that line so you don’t forget later on.
Now, for the next part,
-
Create a file inside your TaskPaper-Ruby library. Now, this is NOT INSIDE YOUR
src
, but the path above. Name it something you would remember. I am going to use,taskpaper-to-mmd.rb
-
Add the following code,
#!/usr/bin/ruby
# TaskPaperRuby
#
# This script converts TaskPaper files to DOCX.
#
# Made by Victor Gutierrez
#
github_url = "http://github.com/mattgemmell/TaskPaperRuby"
#
# ==========================================================
require_relative 'src/taskpaperdocument'
require_relative 'src/taskpaper+mmd'
require 'pandoc-ruby'
# Handle command line arguments
if ARGV.count < 2
puts "Usage: ruby taskpaper-to-mmd.rb INPUT_FILE_PATH OUTPUT_FILE_PATH"
puts "(Input file should be a TaskPaper file. Output will be an Open Doc.)"
exit
end
input_file_path = File.expand_path(ARGV[0])
docx_output_file_path = File.expand_path(ARGV[1])
# Ensure we have an input file to work with
if !File.exist?(input_file_path)
puts "Couldn't find input file \"#{input_file_path}\". ¯\\_(ツ)_/¯"
exit
end
# Load TaskPaper file
document = TaskPaperDocument.new(input_file_path)
# Produce MMD output from document
document_mmd = document.to_mmd
// Now, here we are using the Pandoc ruby library to write a docx file from the mmd file above
File.open(docx_output_file_path, 'w') do |outfile|
outfile.puts PandocRuby.markdown(document_mmd).to_docx
end
// That is all!
Okay. The next step is easy, simply run your script. Something like
ruby ~/location_script/taskpaper-to-mmd.rb ~/location/input_taskpaper_file.taskpaper ~/location/output_docx_file.docx
It should work. Tell me if that helps.