Increasingly complex and obscure code

I’ve spent much of the weekend writing a couple of Emacs functions to automate certain elements of my fic formatting/uploading process; there’s nothing I love more than writing some extremely specific code that will be of no use to anybody but me. Here, nonetheless, are some notes on my new workflow for fic posting!

There are three Lisp functions (one of which I wrote a while ago, but I’ve now made some changes so it integrates better with the other stuff), and a Python script.

org-format

org-format is my function for tidying fic formatting. I write most of my fic on my phone in Google Keep, so it ends up with various typographical issues: straight quotes instead of curly ones, hyphens instead of dashes, etc. When I copy a fic over to Emacs, I save it in a .org file and run org-format to sort this out.

fic-ao3

Org-mode has a really great customisable export function, which lets the user export files to HTML and LaTeX as well as a few other formats that I care much less about. I use the HTML exporter in my DW posting function, and it’s also the central part of fic-ao3, which, as the name suggests, formats the body of a fic to make it ready to post on AO3. This involves stripping out all the HTML metadata (some of this is done through setting variables before the export function is called, the rest through deleting parts of the resulting .html file by means of increasingly convoluted regexps) and then copying the eventual output to my clipboard. After that I can head over to AO3, open the posting form, put in all the metadata, think of a terrible title, and just paste the fic in.

fic-header.py

Over to the command line to run this Python script, which then downloads the metadata from the fic I’ve just uploaded to AO3 using my beloved Beautiful Soup, formats it ready for posting on my masterlist, and saves the output to an HTML file. The fic headers can’t be fully automated because character/fandom names may need to be changed, extra descriptors might be worth adding to the “genre” heading, the notes may be need to be framed slightly differently, etc., but it gets most of the way there (and I added a few lines to the script to replace the canonical AO3 tags for some of my common fandoms and characters with shorter equivalents). As an example, the only thing that needed to be done to the header for the fic I posted this morning was adding the mention of /threesentenceficathon to the notes field.

fic-export

Once I’ve got the metadata ready for uploading, I can go back to the .org file and run the final function, fic-export. This asks for a few details about the fic (whether I want the title exported, whether it’s a multichapter work, and whether chapters have titles if so), exports the .org file as both HTML and LaTeX, and then runs a few more regexps on the exported files depending on the parameters passed in. The HTML file is edited to make the structure work with my modified version of Tufte CSS (basically, replacing div with article and section so that the text fills up the right amount of the page, and replacing footnotes with sidenotes), and other edits are made to the title and chapter titles to match what I asked for when I called the function. Before doing some similar editing on the LaTeX file, it runs a shell command to export this one as an EPUB using pandoc. (Fun[?] fact: I tried calling Emacs in batch mode from the shell and wrapping the whole thing in a bash script, but it ended up being much easier to do the opposite, i.e. including the occasional shell functions I needed as part of the Lisp function and calling the whole thing from Emacs. Also probably a lot more appropriate, because I spend much more time in Emacs than in the terminal.)

Then the LaTeX file is edited, which is pretty much the same thing as for the HTML file: yet more regexps, some of them inside if functions. It’s slightly more brain-melting because LaTeX uses backslashes for its commands, and regex uses them to escape characters, and Lisp also uses them to escape characters, so when I’m searching for \maketitle in the LaTeX file, the ridiculous string I actually need to include in the regexp is \\\\maketitle. But it’s the same kind of thing, making decisions about aspects of the way the file is formatted based on whether I want the title to be present, whether it has chapters, etc. A lot of the settings are included in a header file, and specific options are set in the main LaTeX file that call various things from that header, such as the style of title page. The usual one is pretty minimal for now, so maybe that’s something I’ll tweak in the future.

After all the many if-statements have been worked through, the function calls another shell command to run xelatex on the LaTeX file – I’m sure there should be a way of doing this with an Emacs function, because it’s certainly possible interactively, but whatever I attempted to use in the function didn’t work so I just shoved the shell version in there. This results in a PDF: here’s the one from this morning’s fic, which is pretty basic because it’s a no-title no-chapters affair, but it gives an idea of the design (and also I want to show off that dropped capital because I came up with a very good way of adding those … shockingly, it’s yet another regexp).

Putting it together

The last thing fic-export does is delete the aux files, including the LaTeX file itself, so I end up with ficheader.html, fic.html, fic.pdf, and fic.epub plus the original fic.org. Now I can SSH into my website, open up the fic masterlist, paste in the contents of ficheader.html, upload the others (minus fic.org) to my fic files directory, and that’s it!

This did take … quite a lot of time … to put together, but it’ll certainly save time on formatting fics in the future, and means I get those nice PDF and EPUB versions to put on my website. […]