Programming
Dynamic DNS - auto-updating from macOS
To run a little project (that I’ll describe at some point in the future) I have to run a small web server from my home computer, one that happens to run macOS. More than anything else, this is just a reply of what I did to get it running in case: a) I have to do it again, or b) Someone else can find it useful.
Sign up for dynamic DNS service
I signed up for service with dynv6 because I saw it recommended elsewhere and it didn’t look creepy like some of the other options. I just signed up with email - through an email proxy anonymizer, because I’m paranoid. After verifying my email, I was able to create a new “zone”, basically a record of my public IP address linked to custom DNS.
Fixing CodeRunner jQuery injection
CodeRunner is one of my favourite development environments on macOS. I use it for small one-off projects or for testing concepts for integration into larger projects. But in version 4.0.3, jQuery injection in a custom HTML page is broken, giving the error:
It’s probably due to some unescaped bit of code in their minified jQuery, but I didn’t have time to work that hard. Instead I reported the error to the developer an fixed it myself. The original (default) run script for jQuery is:
Parsing Russian Wiktionary content using XPath
As readers of this blog know, I’m an avid user of Anki to learn Russian. I have a number of sources for reference content that go onto my Anki cards. Notably, I use Wiktionary to get word definitions and the word with the proper syllabic stress marked. (This is an aid to pronunciation for Russian language learners.)
Since I’m lazy to the core, I came up with a system way of grabbing the stress-marked word from the Wiktionary page using lxml and XPath.
Friday, January 29, 2021
Custom aliases in oh-my-zsh
With oh-my-zsh, you can store custom aliases in multiple (?per application) file under .oh-my-zsh/custom giving them .zsh file extensions.1
¶For example, in my hugo.zsh file, I have:
alias hnewtil="/Users/alan/Documents/blog/ojisan/scripts/newtil.sh"
alias gtojisan="cd /Users/alan/Documents/blog/ojisan; ls -l;"Executing inline Python in a shell script
It’s possible using the -c command.2
python -c 'import foo; foo.bar()'Wednesday, January 27, 2021
W3schools.com has a CSS library that’s quite nice. I often use Bootstrap; but I like some of the visual features here better. For example, I like their tags because they have more flexible use of colour.
If you want to fetch from a Python dictionary, but you need a default value, this is how you do it:
upos_badge = {'noun': 'lime','verb': 'amber', 'adv': 'blue',}
badge_class_postfix = upos_badge.get(value.lower(), 'light-grey')I recently learned about DeepL as an alternative to Google Translate. It seems really good.
More on integrating Hazel and DEVONthink
Regex to match a cloze
Anki and some other platforms use a particular format to signify cloze deletions in flashcard text. It has a format like any of the following:
{{c1::dog::}}{{c2::dog::domestic canine}}
Here’s a regular expression that matches the content of cloze deletions in an arbitrary string, keeping only the main clozed word (in this case dog.)
{{c\d::(.*?)(::[^:]+)?}}
To see it in action, here it is in action in a Python script:
import re
def stripCloze(searchText):
return re.sub(r'{{c\d::(.*?)(::[^:]+)?}}', r"\1", searchText)
print(stripCloze("The {{c1::passengers::tourist riders}} spotted a breaching {{c2::whale}}."))It should return The passengers spotted a breaching whale.
Removing stress marks from Russian text
Previously, I wrote about adding syllabic stress marks to Russian text. Here’s a method for doing the opposite - that is, removing such marks (ударение) from Russian text.
Although there may well be a more sophisticated approach, regex is well-suited to this task. The problem is that
def string_replace(dict,text):
sorted_dict = {k: dict[k] for k in sorted(dict)}
for n in sorted_dict.keys():
text = text.replace(n,dict[n])
return text
dict = { "а́" : "а", "е́" : "е", "о́" : "о", "у́" : "у",
"я́" : "я", "ю́" : "ю", "ы́" : "ы", "и́" : "и",
"ё́" : "ё", "А́" : "А", "Е́" : "Е", "О́" : "О",
"У́" : "У", "Я́" : "Я", "Ю́" : "Ю", "Ы́" : "Ы",
"И́" : "И", "Э́" : "Э", "э́" : "э"
}
print(string_replace(dict, "Существи́тельные в шве́дском обычно де́лятся на пять склоне́ний."))This should print: Существительные в шведском обычно делятся на пять склонений.
URL-encoding URLs in AppleScript
The AppleScript Safari API is apparently quite finicky and rejects Russian Cyrillic characters when loading URLs.
For example, the following URL https://en.wiktionary.org/wiki/стоять#Russian throws an error in AppleScript. Instead, Safari requires URL’s of the form https://en.wiktionary.org/wiki/%D1%81%D1%82%D0%BE%D1%8F%D1%82%D1%8C#Russian whereas Chrome happily consumes whatever comes along. So, we just need to encode the URL thusly:
use framework "Foundation"
-- encode Cyrillic test as "%D0" type strings
on urlEncode(input)
tell current application's NSString to set rawUrl to stringWithString_(input)
-- 4 is NSUTF8StringEncoding
set theEncodedURL to rawUrl's stringByAddingPercentEscapesUsingEncoding:4
return theEncodedURL as Unicode text
end urlEncodeWhen researching Russian words for vocabulary study, I use the URL encoding handler to load the appropriate words into several reference sites in sequential Safari tabs.