Some Bits: Nelson's log

9 posts in September 2023

Typographical quotes in 2023

Working on my new blog engine. I want to have “typographical quotes”, ie: “smart quotes”. Nice curly things that look like typesetting, not straight ’ or ” from some old typewriter. I am most interested in American English’s use of double quotes (open and close) and the apostrophe character we use in the middle of a word for contractions and plurals.

Annoyed I have to do this in my HTML; browsers could do a better job here, applying English curly quote rules to text input that looks like straight quotes.

Side note: testing all this was hard because something in Windows, PowerToys, or Windows Terminal is converting stuff I copy and paste. I can have a U+2019 in my terminal window from cat or from a web page and then when I paste it it turns into a U+0022. It was doing this with HTML entities I copied too. So I’m doing cut -c x..y | od -t x1 and dealing with cut not supporting UTF-8.

Side note 2: blogging about all this is impossible. The Wordpress editor is rewriting stuff I type, replacing various crafted markup examples with literal quote glyphs.

Unicode

Unicode double quotes are easy. U+201C and U+201D. Right name, right character category, done. Single quotes used for quoting are also easy: U+2018 and U+2019.

Unicode apostrophe is a mess. The ASCII U+0027 APOSTROPHE is what I type, and has the right name and categorization (“Other Punctuation”). But it looks wrong in most renderings, straight up and down and not curly. It’s kept that way for historical reasons.

U+2019 RIGHT SINGLE QUOTATION MARK is what most people use to get a curly apostrophe-looking thing. It works most places and is labelled in Unicode as “FINAL PUNCTUATION”. Which is half correct; it is punctuation. But also half wrong: apostrophe is not final. Also the Unicode character is actually a quote mark, not an apostrophe at all.

Some folks argue U+02BC MODIFIER LETTER APOSTROPHE is the right thing. But that’s the obscure choice. Also it has category “Modifier Letter” because it’s usually used as a letter, not punctuation.

I think U+2019 is the practical choice, ’, because that’s what most people do already. I assume it’s too late for Unicode to fix this by adding a proper apostrophe. The pedants probably think U+0027 is sufficient.

HTML

The most straightforward thing is just to embed the Unicode in UTF-8. That should be fine in 2023.

The older thing most people do is use Unicode entities so the output is ASCII with HTML encoding. For some reason those tend to be decimal, so you get ’ for apostrophe and “ and ” for double quotes. (Hilarious; I’d typed the entities for 8220, 8221, and 8217 in my Wordpress editor. When I came back what I typed was replaced by the actual glyphs.)

The W3C has a page from 2015 with different advice. It suggests named entities, using “ and ” for double quotes, and ’ for apostrophe. There are also ' and " entities which presumably are to be avoided.

Automatic conversion and Markdown

I’m lazy and want to type ASCII straight ’ or ” and have software convert them to proper quotes. “Smart Quotes”. It’s important to do this in a way that’s aware of HTML markup; you don’t want to be replacing ” in URLs, code fragments, etc.

The CommonMark spec doesn’t seem to say anything about typographer’s quotes. All their test cases replace ASCII ” with HTML entity " but I’m not clear why, or if the spec demands it. Some sort of escaping safety thing I guess? It’s awkward IMHO. This discussion says the reference implementation does some smart quotes but it’s not part of the spec.

In the ancient days we used “SmartyPants” to do a typographic conversion for us. There’s a modernish Python implementation that seems to work. It uses 8217, 8220, and 8221. Good enough for me! But it’s awkward to plug in to other markdown frameworks, in particular smartypants is re-parsing the input and it’d be nice to skip that.

Despite that mistletoe + smartypants works fine.

mistune and marko do not work with smartypants. Both seem to be converting ” into " like CommonMark’s demo tool does and smartypants won’t pick that up and replace them with 8220/8221.

The old pre-CommonMark Python markdown library has an extension called “SmartyPants”. It seems to be emitting named entities like “. It doesn’t do apostrophes?

The Markdown-it-py extension has typographic components for quotes and apostrophes. This seems to work well and as a bonus emits actual UTF-8 for the Unicode themselves, not ASCII escapes. I’m not afraid of UTF-8. That may be enough to get me to use Markdown-it-py!

GNU cut breaks on UTF-8

cut is a very simple shell util; it will take a few characters or fields out of its input. cut -c 10-20 for instance should output characters 10-20 on every line.

Except hahahaha joke’s on you, despite being called --characters and there being a separate --bytes flag, they both actually operate on bytes. No matter what your locale is. This completely breaks UTF-8 input; it does not treat them as characters.

This behavior is not disclosed in the cut --help docs, nor in man cut on my Ubuntu system. It is documented in info cut, the obsolete doc tool rms failed to foist on the world 25+ years ago. There it says:

Select for printing only the characters in positions listed in CHARACTER-LIST. The same as ‘-b’ for now, but internationalization will change that.

There’s a good Stack answer from 2014 explaining all this. I even understand why the GNU tool does this, it’s as much POSIX’ fault as GNU’s. But you’d think in the intervening 9 years they could at least improve the docs.

Here’s a 2006 bug report documenting the same problem. I would not be surprised if the bug goes back to the 90s, predating the adoption of UTF-8 everywhere.

Live reloading HTTP servers

Pressing reload in a browser while developing a web page is for chumps! A very popular trick is to serve the site under development with a special server and put a little Javascript in the page to get a reload signal when it should reload. Saves a lot of time, I love live previews.

five-server is the most mature tool for this I could find. It’s written in Node but you can run it as a standalone server unconnected to the code you’re developing. This is a fork of an older live-server that was abandoned. There’s a VS.Code version too that has some integration that looks useful if you’re editing HTML files.

LiveReload is a Python server to do the same thing. It has a Python API so you can integrate it with your own Python code to help while developing a webapp. I hit a confusing problem because of less-than-robust error handling related to my malformed HTML input (the script injection failed) but it seems to work well.

There must be a zillion other tools that do the same thing.

Markdown processing in Python

I’m mad enough about Hugo I spent an hour writing a very minimal blog engine. Lots to go to make it usable, but as a piece of it I had to look for a Markdown processor for Python. Here’s what I’ve found, there are probably more. I want something that supports CommonMark and has a decent extension story. (A lot of folks support GitHub flavored markdown, perhaps that’s coherent enough to also be reasonable.)

mistletoe. 652 GitHub stars. I’ve used this already and it works but haven’t gotten in deep. Docs and PyPI niceties are not great. Says it’s the fastest implementation but all of these are pretty close (except Mistune which is way faster). Supports rendering to other targets like LaTeX, not just HTML.

markdown-it-py. 517 GitHub stars. Looks comprehensive, has a bunch of plugins. It’s a port from NodeJS code. Plugin code looks pretty complicated.

mdformat. 263 stars. Has plugins and code is connected somehow to markdown-it.

marko. 249 stars. Says it has very easy plugins and is not very fast but correct.

Mistune. 2300 stars. Not CommonMark but maybe reasonably so. Very fast. Plugins look awkward.

Not CommonMark: Markdown, Markdown2,

No real conclusion here. I’ll stick with mistletoe since I already have it working, then look to jump when I get to the part where I need my own plugins. marko or Mistune look most promising to me if mistletoe won’t work.

Update: some very simple evaluation

I did some quick tests comparing mistletoe, marko, markdown_it, and Mistune.

The output of all three were substantially similar. mistletoe emits actual ” ASCII quotes whereas the other two are outputing ". Which reminds me I need to enable typographic quotes.

Some very simple benchmarks show mistletoe and markdown_it takes 2-3x as long as mistune, marko is about 4x.
timeit 9000 byte input: mistune (1.13ms), markdown_it (2.78ms), mistletoe (3.60ms), marko (4.84ms)
1800 HTML posts: mistune (490ms), mistletoe (860ms), markdown_it (925ms), marko (2000ms)

Update 2: markdown_it

After reading docs and setting up typographical quotes I think I’ll go with markdown_it. I was turned off at first because it was a NodeJS port and its code seems quite complicated, they really want to explore details of parsers, ASTs, renderers to you. But it does typographical quotes right! And it’s fast and popular. Now that I’m over the learning hump it seems a good choice. I haven’t tried writing a plugin for it yet though.

Some new Hugo learnings

I’m once again trying to port my ancient Blosxom blog to a static engine. Focusing on Hugo this time. I no longer care which engine I use, just want to use one, and the most popular one is probably a good choice. I am adamant that I have to write my own theme, collection of templates and layouts and stuff. I want complete control over site layout and page contents.

Most tutorials are toys that never talk about pagination. Many have a default home page of “all your posts”. This seems to be for crummy marketing sites with 3 pieces of content and are not useful for a real blog with 1000+ posts. Pagination is a whole complex topic I don’t understand yet.

A lightbulb went off when I looked at this classless-blog Hugo theme. It has no Javascript. It produces really simple semantic HTML with no CSS classes, just clean tags like <header> or <main>. You can then change how the page looks with pure CSS, perhaps starting with one of these classless CSS resets. That is the One True Way to web development, IMHO, at least for simple static textual content. I’m glad to see a clear path to doing it in Hugo. (Seriously, people are stuffing React into Hugo blogs? Ugh.) Unfortunately the classless-blog theme isn’t really suitable for me to just use (it doesn’t have the right structure for me) but I can steal a lot of ideas from it.

As for writing my own theme, I’m finding this tutorial really helpful. It’s from 2020, so a little out of date, but mostly good.

The tutorial neglects to mention at the start that you have to set the theme= in your configuration TOML. That file recently got renamed from config.toml to hugo.toml. Fun fact; if you don’t set that variable, Hugo throws an opaque error message.

WARN found no layout file for “html” for kind “home”: You should create a template file which matches Hugo Layouts Lookup Rules for this combination.

What that error really means is “theme not found so you have no layouts at all for your blog”. It’d sure be nice if the tool caught this simple beginner’s error.

Hugo themes are programmed in Go template languages. Last time I encountered this I got mad but I read the excellent Go docs and now understand it’s pretty simple with just a few control flow items. The weird thing is some syntax like {{if}} are genuine control flow things, others like {{printf}} are more like function calls. Also there’s a {{define}} which creates new functions. The confusing part about Hugo templates is the implicit context hidden in . the cursor and how {{range}} is a looping construct. Also it’s still not clear how often templates are invoked. But...

Hugo has a concept of “Kinds”. These are the various sorts of pages that Hugo will create as it runs and it’s a little confusing and poorly documented. The docs here for .Kind are the most explicit I could find.

The page’s kind. Possible return values are page, home, section, taxonomy, or term. Note that there are also RSS, sitemap, robotsTXT, and 404 kinds, but these are only available during the rendering of each of these respective page’s kind and therefore not available in any of the Pages collections.

Each of these kinds is in turn instantiated by a template file. Hugo will look in many places for the template for a kind, with up to 46(!) different paths. The template lookup order docs are essential for understanding this.

It’d be nice if Hugo had a debug mode which printed every HTML page it created, why (what kind maybe?), and every template that was used to create that. That’d really help understanding what the generator is actually doing. I did find hugo --printUnusedTemplates useful for at least finding when template files weren’t being used.

Global Starlink outage for hours

There was a major outage yesterday across all of the Starlink ISP. Mine went down at 4:38pm California time. I don’t know when it came back (I swapped to my backup Internet.) This article says Starlink acknowledged the problem via a tweet at 5:33pm and that it was fixed at 6:33pm. So that’s two hours, with an hour to acknowledge the problem. Comcastic!

Starlink has no public monitoring page, no online status. There’s nowhere to go to find out what’s happening. Hour late tweets are not sufficient. DownDetector was surprisingly useful but all that does is count how many people are clicking a button that says “something is wrong”. Better than nothing but it’d be nice if Starlink did something real.

I continue to be mostly happy with Starlink service, particularly since the improvement early this year. I respect the complexity of what they’re doing with a satellite network. But some aspects of the product are not very good, particularly customer contact. I’m just grateful I’ve never needed to do a real support request; the experiences reported on Reddit vary wildly.

Python venv brittleness (with fix?)

I just upgraded from Ubuntu 22.10 to 23.04. Which means I upgraded from Python 3.10 to 3.11. Which means all my old Python programs don’t work anymore. Because each has a virtual environment and the venv is hardcoded to Python 3.10.

Am I doing it wrong? Yes, I was doing it wrong.

In the old days I’d create a venv with a command like python3.10 -m venv venv. This results in a venv/bin directory where python3 is a symlink to python3.10 in the same directory, which in turn is a symlink to /usr/bin/python3.10 (which no longer exists).

Awhile back I changed to running python3 -m venv venv. This results in all the binaries in venv/bin being symlinks to python3, which in turn is a symlink to /usr/bin/python3. That should be robust to upgrades. And Python is good about backward compatibility so using 3.11 to run a venv built for 3.10 should mostly be fine.

FWIW the docs just have you invoking python on the command line, so I guess that’s what the intended usage was. More generally it looks like venv is doing its best to set up an environment with the exact same Python pathname you used to create the environment, which seems reasonable enough.

Cronometer tech details: data accuracy

I switched from MyFitnessPal to Cronometer for tracking the calories in the food I eat. It’s a good food diary. Need more time using it but I plan to write a happy review on my main blog. A key advantage in Cronometer is its data is accurate. Some technical notes on details of that:

They use carefully vetted data sources. Most food trackers advertise “14 million foods!” but 13.9M of those are wrong or incomplete data. Cronometer has a core database of 30,000 foods from reliable sources. Most of my entries are coming from the NCCDB, a commercial database with an academic connection. Cronometer also has a broader set of data from commercial sources and the user community but these are clearly labelled as less accurate.

A related advantage is Cronometer has pretty good search ranking. When I type “milk” the top 2 results are “whole milk” and “2% milk”, both from the NCCDB. Good authoritative data. I’m finding mostly generics there too, not brand names which are just unnecessary extra choices for staple items.

Cronometer also shows data confidence scores on its summary statistics like how many calories or how much Vitamin C you ate for the day. Most of my scores are 100% or 90%. It’s a pretty broad accuracy number, mostly they’re just accounting for incomplete entries in the database. But it’s a thoughtful touch.

OTOH they also show calories to a ridiculous level of precision. It tells me the 1 tablespoon of butter I put on my toast had 101.72 calories in it, as if I measured that tablespoon to 5 significant digits and butter never varied. It’s weird they don’t just round to the nearest integer. Most of the UI is very good, this is a rare mistake.

MyFitnessPal data exfiltration

I’m mad about the food tracking app MyFitnessPal for various reasons, it’s a badly designed product. But I’ve been a user of it for a long time and I’d like to get a copy of all the data I laboriously entered. They won’t give it to me.

MFP has a data export feature. But it requires you pay an eye-watering $20/mo to get access to it and it seems limited to exporting 90 days of data.

They have a basic report feature that will show you a graph of calories consumed by day for the last 90 days. That’s the main thing I care about although it’s nowhere near complete. Anyway that’s backed by an XHR for the URL https://www.myfitnesspal.com/api/services/reports/results/nutrition/Calories/90?report_name=Calories which gives you a simple JSON time series. Unfortunately changing that “90” to “365” or “180” results in a server error (after a long delay) so I don’t think you can trick it into giving you more data.

They also have a printable diary report type which gives you line items for every food item you’ve entered. There’s no limit in the UI on how long that report goes for, but the webapp seems to error out (after a long delay) if you get more than about a month of data. Perhaps scanning more than 500 rows in a database is too much Big Data for their system architecture. That data is coming from a POST XHR to https://www.myfitnesspal.com/api/services/diary/report and you can capture a detailed JSON response in browser developer tools. I was able to use this to tediously get my data a month at a time.

I couldn’t find an easy way to get the recipes, meals, etc I’d custom created for myself. I don’t really use those features much so I didn’t try very hard. There’s a lot of other data they collect too, it’d sure be nice to have a comprehensive “download all data” option for what is, essentially, a data recording app.