Anyway, language hell is ahead of me and I'm sure at some point I'll convert it to use a proper API in a proper language. Right now, I need a simple way to do tags. Since I don't use the
description
metadata field (or indeed really acknowledge its existence until I started this) I think I'll use that. Adding something like #npc #story
to the field is the level of work I'm willing to do - what I want is something that will pull these tags and list my files using them.First things first - how on earth do I start writing Google Scripts? Obviously as a lovingly maintained part of the Google suite it'll be tightly integrated so all I have to do is ... add what it claims is a plugin to Drive and I can start creating script files which open in an in-browser editor (I hate in-browser editors, but here we are). Then I need an index file and something to call it. It seems that when loading, the
doGet
method in the Code.gs
file is called and that needs to hit the HTML file. This all needs accessing via a URL to the dev or published version. Ok:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doGet() { | |
return HtmlService | |
.createTemplateFromFile('index') | |
.evaluate(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<base target="_top"> | |
<link rel="stylesheet" media="screen" type="text/css" href="https://www.tomnatt.com/css/tomnatt.css" title="Default"> | |
<style> | |
h1 { | |
font-size: 36px; | |
-webkit-transform: none; | |
transform: none; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="content"> | |
<h1>Elsewhere docs</h1> | |
<? var tags = getTags(); | |
for (var i = 0; i < tags.length; i++) { | |
var docs = getDocsForTag(tags[i]); ?> | |
<h2><?= capitalizeFirstLetter(tags[i]) ?></h2> | |
<ul> | |
<? for (var j = 0; j < docs.length; j++) { ?> | |
<li><a target="_blank" href="<?= docs[j].getUrl(); ?>"><?= docs[j].getName(); ?></a></li> | |
<? } ?> | |
</ul> | |
<? } ?> | |
</div> | |
</body> | |
</html> |
Google Scripts provides a load of objects to poke and since it's taking care of the authentication (the main reason I'm doing it this way rather than using the api) this should be pretty straightforward. I mean the only way this could go wrong is if the scripting is using Javascript from before the dawn of time and I have to do absolutely everything myself?
Argh.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function capitalizeFirstLetter(string) { | |
return string.charAt(0).toUpperCase() + string.slice(1); | |
} |
Under the hood ... eugh. It's pretty ugly - and not just because I've used two different forms of a forEach loop in a single short script. To generate this page, I'm looking at each file to get a list of unique tags then reversing the process - looking at each file to figure out if it has the tag I'm interested in. This is not Efficient CodeTM. On the other hand, it is Working CodeTM so it'll do as a proof of concept. I'll make it less awful when it gets too slow or I translate it all over to the API.
In the meantime, I have a document tagging system, and I can still use my favourite online text editor, even if I had to write the code in my less than favourite online code editor. And here is the code all in one place. One day I'll get tagging and Markdown support from Google itself...