Writing Pinyin and Chinese
8/10/2021
Writing Chinese with a Computing Device#
Today's post is mostly about how to write 拼音 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:
- Dependency to
pinyin-utils
npm module. This code could prefectly well be self-contained with no dependencies - Pinyin characters need to be separated by space, so
pin1 yin1
is OK, whilepin1yin1
is not. No reason for this besides that I want 0.1 version out already today - Not using definition yet, my idea is to have optional definition part that would generate
- : The Description List element (MDN)
- 拼音
- pinyin is the official romanization system for Standard Mandarin Chinese in mainland China
- No proper styling yet (classes for elements etc.)
Additional links#
For future reference:
- Pinyin Pronunciation-Learn Rules of Using Pinyin Tone Marks that includes rules for placing the tonal marker
- test page for displaying pinyin tone marks with Unicode