Generating Roman numerals in Vala

I have recently been learning and enjoying the Vala programming language.  I am writing a lightweight markdown-to-PDF converter and wanted to be able to automatically number list items in Roman numerals.  Here, in case anyone wants it, is Knuth’s algorithm for producing the Roman numeral for a number.  I converted this to Vala from the original WEB source, part of TeX, as quoted by Hans Wennborg.  Enjoy!

string roman(uint num)
{
    // Knuth's algorithm for Roman numerals, from TeX.  Quoted by
    // Hans Wennborg at https://www.hanshq.net/roman-numerals.html.
    // Converted to Vala by Chris White (github.com/cxw42).  CC-BY 4.0 Intl.

    var sb = new StringBuilder();

    string control = "m2d5c2l5x2v5i";
    int j, k;   // mysterious indices into `control`
    uint u, v;  // mysterious numbers
    j = 0;
    v = 1000;

    while(true) {
        while(num >= v) {
            sb.append_c(control[j]);
            num -= v;
        }
        if(num <= 0) {  // nonpositive input produces no output
            break;
        }

        k = j+2;
        u = v / control[k-1].digit_value();
        if(control[k-1] == '2') {
            k += 2;
            u /= control[k-1].digit_value();
        }

        if(num+u >= v) {
            sb.append_c(control[k]);
            num += u;
        } else {
            j += 2;
            v /= control[j-1].digit_value();
        }
    }

    return sb.str;
} // roman()

(not extensively tested — use at your own risk. No warranty. License details here.)

Cheese of the Day

Craigs Creamery Sharp Cheddar Chunk – craigscreamery.com/our-products/sharp-cheddar-chunk/

Similar to a Cabot sharp, but a bit smoother. I plan to add it to my rotation for a while. I have not yet cooked with it, so have no culinary data.

As always, I have no connection whatsoever with the product or purveyor.

Some bookmarklets

Edit: For the “welcome to Stack Overflow” bookmarklet, go to this jsFiddle, hit Run, and then drag the resulting link to your bookmarks bar.

A custom tea timer for https://smithtea.com/pages/know-your-tea#brewing-instructions:

javascript:(function(){var timestr=window.prompt("Time in sec?","120"); if(timestr===null) /*Cancel*/ return; clock.stop(); clock.reset(); var t = +parseInt(timestr,10) || 0; /*https://stackoverflow.com/a/7540412/2877364*/ if(t<=0) return; clock.setTime(t); clock.start(); })();

A message I have found myself typing frequently on Stack Overflow: 🙂

javascript:!function(){var t=document.querySelector.bind(document),o=t(".edit-post");if(o){var e=t(".js-add-link.comments-link");if(e){e.click();var a=t('textarea[name="comment"]');a&&(a.value="Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for more about asking questions that will attract quality answers. You can [edit your question]("+o.href+") to include more information.",a.scrollTop=a.scrollHeight,a.focus())}}}();

As compressed by https://jscompress.com/; non-minified is:

javascript:(function(){
 var qsel = document.querySelector.bind(document);
 var edit_link = qsel('.edit-post');
 if(!edit_link) return;
 var add_comment = qsel('.js-add-link.comments-link');
 if(!add_comment) return;
 add_comment.click();
 var textbox = qsel('textarea[name="comment"]');
 if(!textbox) return;
 textbox.value =
"Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) "+
"and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for "+
"more about asking questions that will attract quality answers. You can "+
"[edit your question](" + edit_link.href + ") to include more information."
 textbox.scrollTop = textbox.scrollHeight;
 textbox.focus();
})();

These are the first two I have written.  I  hope they are useful to you, or serve as helpful examples if you are writing your own bookmarklets!

Can you iterate JavaScript object properties in insertion order?

Yes.  Yes, you can.  At least as of ES2015.  I found this out when working with the jstree contextmenu plugin.  That plugin receives an object with named keys for the items to show in the menu.  It assumes that jQuery’s $.each will traverse those items in the same order they were added to the object.  $.each boils down to a for-in loop.  Well, turns out that since ES2015, the for-in loop returns properties in a deterministic order!  As long as your keys are strings that can’t be parsed as integers, you will get them out in the same order they were added.

Useful links:

This relates to my new Google Chrome extension, TabFern, about which more news soon.  If you have lots of browser windows and tabs open, or want to manage multiple browsing sessions, TabFern may be just what you are looking for!

NBSP in Word, part 2

This is a follow-on to my previous post on the subject.  These are my notes as I’m working, so they don’t read very well as text 🙂 .

Another test, on a different file exhibiting the same problem:

In the UI:

  • Removed all the new-style equations (OMath objects)
  • Changed all OpenType fancy features to defaults (in the Character Properties)
  • Changed the proofing language for all text to English

In word/settings.xml:

  • Removed FELayout
  • Removed all w:compat/w:compatSetting except for [@w:name=”compatibilityMode”]
  • Left m:mathPr for now, but I may remove that another time. Edit: I removed that below.

Did `find . -name \*xml | xargs grep -il asia`.  That gave me word/{document,numbering,settings,styles}.xml.

Used XML Notepad 2007 to move tags onto their own lines — just open the file, then save it.

settings.xml: it was w:themeFontLang.  Leaving that in for now.

styles.xml: It’s a bunch of <w:lang/> and <w:rFonts/> (`grep -i asia word/styles.xml |sort|uniq|less` is my friend!).  Leaving them in for now.

numbering.xml: Again, just <w:rFonts/>.  Leaving them for now.

document.xml: There were two lonely <w:rFonts/> tags!  Those I left in at first.

Zipping it

I tried using 7z, both from the context menu and with `7z a -tzip -mm=Deflate -mx=0 ../draft8e.docx *` (7z command-line options reference).  However, Word 2013 didn’t like either.  I used Send To | Compressed Folder, and that was OK.  A problem for another day.

More changes

Result?  Still no luck.  Time to strip the rFonts!

  1. Remove the rFonts referencing asia (/asia/g, in fact) from document.xml.  Result? No luck.
  2.  Remove that pesky m:mathPr from settings.xml.  You know, I wonder…  Result?  No luck.
  3. Remove w:themeFontLang from settings.  In styles, leave w:val, but remove w:eastAsia and w:bidi.  Result: success!!!!

So if you do all of the above, you’ll probably be OK 🙂 .

Edit 2017/11/02

More discussion of this issue, in a LibreOffice context.  Apparently Word 2016 has gone back to fixed-width NBSPs.  I hope it is still fixable!  I will probably have 2016 by the end of the year and will post updates then.