Timdams's Bloghttps://timdams.com
.NET, Windows, Raspberry Pi, Markdown and other stuff that interests meFri, 18 May 2018 19:15:47 +0000enhourly1http://wordpress.com/https://secure.gravatar.com/blavatar/2d2cd7f67b98b12ca1176a541ca94e8a?s=96&d=https%3A%2F%2Fs0.wp.com%2Fi%2Fbuttonw-com.pngTimdams's Bloghttps://timdams.com
Exporting open office with equations to markdown and mathjax/tex formulashttps://timdams.com/2018/05/18/exporting-open-office-with-equations-to-markdown-and-mathjax-tex-formulas/
https://timdams.com/2018/05/18/exporting-open-office-with-equations-to-markdown-and-mathjax-tex-formulas/#respondFri, 18 May 2018 13:25:13 +0000http://timdams.com/?p=702Well, that’s the most uninspirational title ever. Especially because this is the first blogpost in ages on here. Anyhow….

The problem:

So a colleague of mine wrote a very hefty 300 pages tome on electric fundamentals. It is written in Microsoft Word but, being the 21th century and all, we really would love to have this syllabus also as an online gitbook.com-site. However, it contains 700+ equations that just won’t get converted.

When we used gitbook-convert on the docx the output it generated was okay-ish (though the image uris needed some manual labor afterwards). However, no equations to be found whatsoever, it simply skipped those like a lazy student.

The solution:

After dicking around with several “solutions” from stackoverflow we finally managed to get the solution ourself. As a fair warning: I suck/can’t use regex so you will see some cringy stuff down here…but hey, it works and that’s what count!

How we solved it:

Saved the .docx document as an open office document (ODT) from within Microsoft Word.

Send the odt through my epic code (seen below) which does:

Unpack the odt file (it’s just a zip with lots of xml-files)

Identify the equations in the document

Transforms the equations to Mathjax compatible versions

Insert transformed equations into odt

Repack everything to an odt file

Send the odt through gitbook-convert

Profit!

Gimme epic code!

Ok, so step 2 was ofcourse the main problem. Here’s “a solution”, but as warned it’s just a quick’n dirty fix.

Step 1: Unpack the odt file

System.IO.Compression.ZipFile.ExtractToDirectory(source, tempfolder);

Easy huh

Step 2: Identify the equations in the document
Using a very science-y way we discoverd that all equations are conveniently inside subfolders called “Object x” (x being a number) in which the actual equations is described in a separate content.xml document using mathml, the openoffice way of describing equations.

So we used a simple loop over all extracted folders to identify the xml-files we needed.

Step 3: Replace the equations in the document with Mathjax compatible version
This is the meaty part of the solution.
There’s several XSL-files to be found online that transform MathML to MathJax/Tex format. So we used one such as this one.

So first we iterate over all the found xml-files from the previous step and transform them using the xsl-files:

foreach (var item in files)
{
var myXslTrans = new XslCompiledTransform();
myXslTrans.Load(@"mmltotex\mmltex.xsl");
using (StringWriter sw = new StringWriter())
using (XmlWriter xwo = XmlWriter.Create(sw, myXslTrans.OutputSettings)) // use OutputSettings of xsl, so it can be output as HTML
{
myXslTrans.Transform(item, xwo);

Next we need to safe this transformed equation so we can later on inject it inside the actual odt-document.

Before saving the transformation I also cleaned it up a bit so that gitbook won’t start crying like a little baby (It’s not very happy with two opening curly braces next to each other and with multiline equations). After cleanup I save each equation in a dictionary with the foldername being the key since this is the same id the main odt-document (content.xml in the root of the odt/zip) used to pinpoint to the xml files in the Object-folder (long sentence, too tired to write out :p) .

And finally I add the much needed extra dollar signs since my xsl only adds one and we definitely need two at the start and end:(

In this short guide I will demonstrate how to create a simply outlook-plugin (VSTO add-in), consisting of a button that can be clicked in the Ribbon-bar and that will result in some custom action (in this case saving all attachments to your computer).

The reason I wrote this guide is because the standard documentation didn’t really go all-the-way and/or went too deep too fast while creating a plugin I needed (a “Send All Attachments to Calibre” add-in, more info here).

Hence this epic guide.

Let’s go after the fold!

Creating a project (in VS 2015)

Choose an “Outlook 2013 and 2016 VSTO Add-in” project , which can be found under Templates-> Visual C# -> Office/Sharepoint(*shudder*) -> Office Add-ins

If you can’t find the project template, make sure to customize your VS installation by running the setup again.

Statup code…we don’t need that

The boilerplate code that is now shown in the .cs file can be ignored. The startup-method can be used if you need certain stuff to happen every time your plugin loads. Just remember that this usually happens when outlook starts, so make sure you don’t do too much heavy lifting here.

Add a custom ribbon

In this plugin we want our own shiny ribbon and button in outlook. Rightclick your project and choose “Add new Item…”. You can create a Ribbon using XML, or go for the lazy, yet perfectly acceptable way using the Designer. Guess what we choose?! Correct, let’s choose “Ribbon (Visual Designer)”. For now, Ribbon1 is a perfect name for this Ribbon.

Visual Designer: helping students learn to code since 1999.

All the code and design stuff will happen in this newly created file.

RibbonType…Say what?!

Depending on where and when you want your ribbon to show up in Outlook, you might need to change the RibbonType of this new class. DON’t FORGET THIS (I did and had to search far and wide for my Ribbon..it just wouldn’t show up).

Select your Ribbon by clicking in the designer on the title of the Ribbon, here “Ribbon1”. In your properties window, you should now see all the properties of your ribbon, including RibbonType. As you can see, there’s a lot of places and moments you might want to show your Ribbon to the user.

Choose wisely, or you might never see your ribbon again!

Some interesting RibbonTypes include:

Microsoft.Outlook.Explorer: show ribbon on the main window

Microsoft.Outlook.Mail.Compose: show ribbon when composing a new email

Microsoft.Outlook.Mail.Read: shown only when opening a mail in a new window (double-clicking mail in explorer view)

Let’s go for Mail.Read.

Add a button

Again in the designer, open your Toolbox. All the usable elements can be found in the “Office Ribbon Controls”. Drag a button to the groupbox (and feel free to change the name of the group and tab. Remember: experimenting is key!)

What would our life be without some buttons to test new code?!

Test what we got!

Ok, let’s see what we got. Make sure to close any running Outlook instances and click the Start+Debug button (F5 for you keyboard-junkies).

Hey, where’s my new Ribbon?!

Remember that we changed the type to Mail.Read. So, go to your inbox (or any other folder with emails) and double-click on of those spam-mails.

There it is!

Yup. There it is.

For reasons unknown to me, if you happen to have other custom tabs, your new button/groupbox might be added to a previously added tab.

It’s not here? Let’s try and fix that.

If you don’t see the ribbon, first make sure you set the RibbonType to the correct type.

Also, right-click your Ribbon and choose “Customize ribbon…” and make sure you can find “Click me” in the “All tasks” lists.

Sorry for the dutch UI.

What also might happen from time to time is that Outlook has disabled your plugin because it crashed previously. If you start coding , you might from time to time create code that creates unhandled exceptions, if that happens, outlook will block the plugin. Make sure to click the Yellow Ribbon on top of Outlook that asks if you want to (re-)enable the blocked plugin!

Write some code: Let’s save the attachment with an Inspector

Ok, time for some code. Close outlook and come back to Visual Studio.

Double-click the button in the Designer. Aah, good ol’ C# (or VB, if you’re one of those…). Fell free to add something lame as MessageBox.Show(“It works!”); and run your project to see it working. Neat huh. From here on you can basically do anything you can do in .NET (use System.IO, dick around with SQL, harvest passwords, whatever… it’s all yours).

However, if you really want to interact with Outlook, you will have to talk with the Inspector class (in Microsoft.Office.Interop.Outlook) and all the need classes such as MailItem etc.

The following fancy code for example simply checks if the current email has any attachments. If it has, it will save all the attachments to a temp-folder (pro-tip, need a temp-folder, use : System.IO. Path.GetTempPath()). Check it out:

So what happens here should be pretty straight-forward. The button that gets clicked sends the Context (i.e. where it was clicked) along with the event (in e) and we cast that to our beloved Inspector (i.e. the window currently showing the Outlook item). We then pray and hope it’s an email, and so cast it to an MailItem. From there on we have all the information of the current email (Subject, Body, Flags, etc) including the Attachments, if any.

That’s it?

Yup, that’s pretty much it. As you will discover, Outlook (and the other Office programs) are very customizable. Just have a look at all the interfaces in Office.Interop.Outlook (here) and discover how you can customize every nook and cranny. Have fun!

Wait how about other Office programs?

Just for the sake of lazzyness, I gathered some example code how to do similar stuff in PowerPoint (all code here is from fora and not mine!)

]]>https://timdams.com/2017/05/09/how-to-create-a-simple-outlook-vsto-addin-a-step-by-step-guide/feed/4timdamsSend Attachment To Calibre-plugin for Outlook 2016https://timdams.com/2017/05/08/send-attachment-to-calibre-plugin-for-outlook-2016/
https://timdams.com/2017/05/08/send-attachment-to-calibre-plugin-for-outlook-2016/#commentsMon, 08 May 2017 15:29:16 +0000http://timdams.com/?p=683Wrote a small Outlook 2016 plugin (VSTO) that will easily send all attachments in an email to your Calibre database. All code and the installer can be found here: https://github.com/timdams/SendToCalibre_OutlookPluginVSTO

To business: I recently bought two 8bitdo Nintendo Controllers (8Bitdo Crissaegrim NES30 PRO) to use with RetroPi (works like a charm by the way).

However, to get them to work on my computer was something else.

The controllers don’t talk very nicely with your PC, so Steam and Monogame basically say ‘$$$$ you’ when you try to use the controller. However, the solution came in the form of this nifty tool: x360ce.

Ok, now what? Play steam games?!

The thing to know with x360ce is that you need to place the executable in each folder of the game(s) you’d like to play with your computer.

Paste the exe there, launch it, let it create the necessary files, test the controller and presto: you can now launch the game (from within steam if you also want your steam controllers to work) and the games will now detect your controller as if it were an Xbox 360 controller. (protip: this also works if you’re using the controller wireless, even with two of them connected without wires)

Pressing lots of stuff on the controller and simultaneous trying to take a screenshot…not easy, try it yourself!

Monogame as well?

Yup, the same trick here. Simply put the x360ce exe in your debug/bin folder, launch it, and suddenly you can type fancy code such as (source) :

if (capabilities.IsConnected)
{
// Get the current state of Controller1
GamePadState state = GamePad.GetState(PlayerIndex.One);
// You can also check the controllers "type"
if (capabilities.GamePadType == GamePadType.GamePad)
{
if (state.IsButtonDown(Buttons.A))
Exit();

Hooray for this. On to toying around with this sweet nostalgic controller

]]>https://timdams.com/2017/01/05/8bitdo-controller-input-in-monogame-and-steam/feed/1timdamsnes30-proPressing lots of stuff on the controller and simultaneous trying to take a screenshot...not easy, try it yourself!A cheaper Freewrite: some linuxnoob tipshttps://timdams.com/2015/06/09/a-cheaper-hemmingwrite-some-linuxnoob-tips/
https://timdams.com/2015/06/09/a-cheaper-hemmingwrite-some-linuxnoob-tips/#respondTue, 09 Jun 2015 18:37:51 +0000http://timdams.com/?p=657Not having the funds to buy myself a Freewrite (formerly known as HemingWrite) I blew the dust from an old, but still working netbook (Samsung N150) and followed this great tutorial “How to turn your laptop into a typewriter“.

Here’s some handy tips if you want to redo the tutorial yourself

After install black screen

Apparently the Ubuntu Server is very barebones and on my netbooks it simply boots to a blank screen with a blinking cursor. Nothing more. To get started, you need to openup a terminal using ctrl+alt+F1. Yeah, I’m a linux noob.

Only boots using usb stick

I followed the tutorial as is and discovered that my fresh Ubuntu wouldn’t boot unless I inserted the original USB-stick from which I had installed Ubuntu in the first place. The problem? The Ubuntu installer installed Grub on the stick instead of on the master harddisk.
This can be remedied simply by using the command:

sudo grub-install /dev/sda

Autologin and boot to terminal

The part about configuring tty1 to automatic login doens’t work anymore in more recent Ubuntu Server version. What you will need to do is oveeride the getty.service, as explained here.

Connecting to wifi is real hard

Most modern wifi networks have WPA or WPA2 security, which is a bit of a pain in the a$$ to get connected to using a terminal-only shell.
Luckily a nifty tool called “nmtui” exist which is basically a text-based UI network configurator.It can be installed using:

sudo apt-get install network-manager

and can then be started by typing:

nmtui

Getting battery status

]]>https://timdams.com/2015/06/09/a-cheaper-hemmingwrite-some-linuxnoob-tips/feed/0timdamsTwoway databinding to a MongoDB collection in WPFhttps://timdams.com/2015/03/13/twoway-databinding-to-a-mongodb-collection-in-wpf/
https://timdams.com/2015/03/13/twoway-databinding-to-a-mongodb-collection-in-wpf/#commentsThu, 12 Mar 2015 22:55:40 +0000http://timdams.com/?p=650I’ll show how to have a two-way databinding between a templated listbox and a MongoDB collection.
I’m finally got around toying with MongoDb.What I’ll show next might not be the most correct way, but it works for me.

I created an Entity class (can be a composite if you wish) which is is simple POCO with INotifyPropertyChanged implented. This Entity class well be serialized to my MongoDB collection as-is. Explained here, for example:

public class Entity
{
public string Naam { get; set; }
}

Retrieve the data and databind

Next up, we need to retrieve the data using a query (coll being my collection a retrieved earlier on (not shown)):

var query = coll.AsQueryable<Entity>().ToList();

The ToList part is important.This will ensure that we get a simple list of Entity objects as our ItemSource and not an IQueryable, otherwise the next part won’t work.

Write changes to database

You can now edit your data in your listbox and once you are ready, you can update all the changes to your MongoDB collection:

Intro

I recently bought a Smappee and I really love the device. It enables me to track the power usage of all my devices throughout the house and I even found some nasty buggers who consume way more than they should. Currently only Android and IPhone users can really benefit from this device (the my.smappee.com webapp is too lightweight to really utilize the full power of the device).

I’m planning on writing a simple Windows Phone 8 app (and perhaps Windows 8) to have some basic information Smappee data on my phone. Unfortunately the API is pretty meagre (you can’t access everything needed), so I won’t be able to recreate the existing Android app. The full API documentation can be found here.

Getting started

Before you can use the public API, you will first need to request the credentials (client_id and cleint_secret) from Smappee, explained here. Once that is done, you’ll also need to have an active my.smappee.com account, which you can create using the Iphone or Android app (you can’t do this through the website unfortunately). These 4 credentials will be needed to access the API.

What’s missing

This code was build a few minutes ago and a lot is missing. Specifically:

No exception handling whatsoever

The Actuator duration can’t be set, because the API has a strange way of doing this (you can give only the value 300,900,1800 or 3600 , specifying the time in seconds the actuator
should be turned on. Any other value results in turning on for an undetermined period of time. )

No logic is added to refresh the token once it’s expired.

]]>https://timdams.com/2015/02/05/a-c-portable-class-library-to-talk-with-smappee-api/feed/0timdamsSmappee-with-HandMy year in review: a not so good year…or just bad.https://timdams.com/2014/12/30/2014-in-review/
https://timdams.com/2014/12/30/2014-in-review/#respondTue, 30 Dec 2014 09:42:30 +0000http://timdams.wordpress.com/?p=6392014 was a very, very meagre year on my part concerning blogging. Did I fall asleep this year? Far from it. A lot has been going on, I just didn’t create a lot of content interesting enough to be posted here. Will it be better this year? Can’t make any promises, but it will be very hard to do worse than 2014: 2 small posts at the start of the year, and that’s that. Shame on me! At least I prepared myself for the new year: I basically deleted my Facebook account (only still alive to follow our neighbourhood group and to use as universal login), removed a lot of distracting stuff from my pc and I’m even considering putting some constraints on my Steam( damn you CS:GO) and Origin (damn you Titan) accounts. My giganto-normous pile of books on my desk is also being trimmed down…one can only read so many stuff in parallel.

Off to a new year. To keep in the spirit of 2014 however I won’t make any brash, numerical promises on what I will do the next year…Happy new year everyone!

Read on if you want a summary of my blog this year

The WordPress.com stats helper monkeys prepared a 2014 annual report for this blog.

Here’s an excerpt:

The concert hall at the Sydney Opera House holds 2,700 people. This blog was viewed about 16,000 times in 2014. If it were a concert at Sydney Opera House, it would take about 6 sold-out performances for that many people to see it.

]]>https://timdams.com/2014/12/30/2014-in-review/feed/0timdamsWhat’s on my Windows Phone – my favourite appshttps://timdams.com/2014/01/17/whats-on-my-windows-phone-my-favourite-apps/
https://timdams.com/2014/01/17/whats-on-my-windows-phone-my-favourite-apps/#commentsFri, 17 Jan 2014 08:47:53 +0000http://timdams.com/?p=622I’m often get asked what apps I have installed on my phone. After explaining I have a Windows Phone 8 (Lumia 720) the question usually results in an awkward “erm..okay, nevermind”. But sometimes a fellow Microsoft enthousiast can be found. It is for these people that I have compiled a list of the apps I have on my ‘start menu’ (e.g. use-a-lot-apps) and those installed (e.g. handy-apps) So without further ado:

Use-a-lot WP8 Apps

The apps I use daily to weekly are added to my startlist. I list them here from top to bottom (I omit the classics like Telephone, People, Calendar, Messaging,Mail, etc)

AccuWeather (including Lock screen ): live tile to know the current weather when you’re in a room without windows.

Taptitude: if ignore the oldschool graphics you’ll discover the countless hours of fun you can have with the one.

]]>https://timdams.com/2014/01/17/whats-on-my-windows-phone-my-favourite-apps/feed/2timdamsBook review: Windows Phone 8 Game Developmenthttps://timdams.com/2014/01/04/book-review-windows-phone-8-game-development/
https://timdams.com/2014/01/04/book-review-windows-phone-8-game-development/#respondSat, 04 Jan 2014 08:24:25 +0000http://timdams.com/?p=615“Sir, I want to be a game developer, what do I do?” is a question I got asked a lot during classes. The honest answer I usually give is :”work hard, keep trying and never stop trying”. I’ll keep on answering this, but I might also recommend a new book that has recently arrived titled “Windows Phone 8 Game Development – A practical guide to creating games for the Windows Phone 8 platform” by Marcin Jamro (Packt Publishing).
Remember the big bad-ass books by Andre Lamothe on (3D) Game programming, where many aspects of game programming were discussed? This new book has the same approach: more is better, plain simple.

A first glance at the index will immediately show the author is trying to describe all aspects of game development, not only the game (engine) coding itself, including social interaction, webservices, publishing, etc. What especially triggered my attention was chapter 3 titled “Mvvm design pattern”, a pretty ballsy approach because up until now I haven’t seen a single book on game development where this pattern is even just briefly mentioned.
Throughout the hefty 394 pages, the author will show how to (re)create a full-blown 3D space , asteroid-like shooter from the ground up. This is both good and bad news: bad because it means most chapters will depend on code of the previous ones, good because it shows how interdependent a lot of aspects are when developing a game. Many books who use this approach (i.e. build an application from start to end) sometimes fails because a) some important code-parts are missing or b) the code is to complex: the reader misses the keypoints the code wants to convey and thus loses time trying to get the code to work AND understand.
I haven’t made the game itself so I can’t vouch for the fact that a workable game can be made following this text but the code itself in the text is well-documented and clear.
A nice addition is the fact that a hybrid approach is used: managed code to harbour the menu/UI-related stuff, and a native part for the game itself. The game itself is written using DirectX, whereas the main game menus are in plain XAML (hence the MVVM approach mentioned earlier).

The text itself is very detailed, every step is explained, but sometimes a bit redundant: the author even explains how to place controls from the toolbox in your designer and how to debug using locals, autos, etc. A tad overkill? On the other hand, this makes the book ideal for anyone new to c#/xaml programming. So it could actually be used as an fun, introductionary book for experienced non-C#-developers who never used Visual Studio.

And yes, a lot is tackled: even augmented reality (AR) is in it: GPS coordinates of other players nearby are used to show their actual location when watching through the camera of the phone.
So in overall: if you need a primer on 3D/3D game programming for Windows Phone 8, this book is a must-have!