Writing Pinyin and Chinese

8/10/2021

Writing Chinese with a Computing Device#

Today's post is mostly about how to write 拼音(pīn yīn) with Mac and with Eleventy.

There is a good, comprehensive post with detailed instructions on how to write Chinese charaters in general on computers and phones at Yoyochinese.com. I am specifically interested in adding pinyin and tone markers along with the hanzi characters to help with pronunciation.

Ruby HTML Element (Boring Technical Stuff)#

I've been vaguely aware of ruby HTML element (no relation to Ruby programming language) that according to MDN article on ruby element:

The <ruby> HTML element represents small annotations that are rendered above, below, or next to base text, usually used for showing the pronunciation of East Asian characters. It can also be used for annotating other kinds of text, but this usage is less common.

Excellent, just what we wanted. Now we can easily add Template Shortcodes on our Eleventy site that generates following ruby HTML element

<ruby>
拼音<rp>(</rp><rt>pīn yīn</rt><rp>)</rp>
</ruby>

From a Nunjuck short code pinyin "拼音", "pin1 yin1"

Version 0.1#

Code for this first version of this Ruby utility is pretty simple:

const pinyinUtils = require("pinyin-utils");

// omitted stuff

eleventyConfig.addShortcode("pinyin", (hanzi, pinyin, definition) => {
const pinyined = pinyin.split(" ").map(pi => pinyinUtils.numberToMark(pi)).join(" ");//pinyinUtils.numberToMark(pinyin);
// version 1 uses only tag, and pinyin-utils
const ruby = `<ruby> ${hanzi}<rp>(</rp><rt>${pinyined}</rt><rp>)</rp> </ruby>`;
if (definition) {
return `<div>${ruby}</div>`;
} else {
return ruby;
}
});

For the moment, it has following flaws I can live with for now:

拼音(pīn yīn)
pinyin is the official romanization system for Standard Mandarin Chinese in mainland China

For future reference: