View on GitHub

scriv-git-pages

Writing in Scrivener for GitHub Pages

Refactoring First Top Better Links

Image References

Here I am at the coffee shop. My cunning plan is to add a suffix line to the end of the whole file, which will look to our Ruby code like just another separator. Then, after chunking the file, the last chunk should be the references. I’ll remove the last chunk from the array, saving it elsewhere, and as I loop through the remaining chunks, I’ll append the reference chunk.

This should be a piece of cake. First the suffix. I think that’s in the compile format. Sure enough, here’s what I did:

Then I ran my splitter to see that it is currently producing files up to 16.md. Once I compile this, it should go to 17 (and probably with a useless title because there won’t be one). Here goes a compile …

Ha ha, jokes on me, it got 18, because before I compiled, this section wasn’t in there either. So it looks good so far. The last file broken out looks like this:


[ScreenShot2018-06-17at5.43.41AM]: /scriv-git-pages/ScreenShot2018-06-17at5.43.41AM.png

[ScreenShot2018-06-15at3.48.45AM]: /scriv-git-pages/ScreenShot2018-06-15at3.48.45AM.png

[ScreenShot2018-06-15at3.56.55AM]: /scriv-git-pages/ScreenShot2018-06-15at3.56.55AM.png

… (and so on)

[Prev](17.html) [Top](index.html) 

OK, looks good. Now to fiddle the Ruby script. [A few moments pass …] Ah, and here it is:

#!/usr/bin/env ruby -wU
require 'tempfile'

SPLIT_MARKER = "----\n\n"

def make_file_name(filenumber)
  return "index.md" if filenumber == 0
  return sprintf("%02d.md", filenumber)
end

def make_link_line(filenumber, max_length)
  link = ""
  if ( filenumber == 1 ) 
    link += "[Prev](index.html) "
  elsif filenumber > 1
    link += "[Prev](%02d.html) " % (filenumber - 1) unless filenumber == 0
  end
  link += "[Top](index.html) "
  link += "[Next](%02d.html)" % (filenumber + 1) unless filenumber >= max_length
  return link
end

def write_file(chunk, reference_chunk, filenumber, max_length)
  filename = make_file_name(filenumber)
  title = chunk.split("\n")[0]
  puts filename + ": " + title
  tf = File.new(filename, "w")
  tf.print chunk
  tf.puts
  tf.puts
  tf.puts make_link_line(filenumber, max_length)
  tf.puts
  tf.puts
  tf.print reference_chunk
  tf.puts
  tf.puts
  tf.close
end

input = ARGF.read
chunks = input.split(SPLIT_MARKER)
reference_chunk = chunks.delete_at(-1)
filenumber = 0
chunks.each do |chunk|
  write_file(chunk, reference_chunk, filenumber, chunks.length - 1) unless chunk.length < 1
  filenumber += 1
end

It went as planned, oddly enough. Remove and save the last chunk, pass it into the write_file function, and append it, with a few return characters, to each chunk as we write it out.

I’m calling that done. For now, I’ll update the table of contents, which is still done manually, then compile this all one more time, split it up, and move it into the real project, commit, and push.

To come: set up this post processor to run automatically, and aim the whole thing at the real folder, assuming Git will let me rename the top folder to be something_mmd. If not, regroup.

After that, maybe table of contents and improving the inter-article links. See you soon!

Refactoring First Top Better Links