Category: Uncategorized

  • Setting alternate hreflang in WordPress

    UPDATE 2013-05-03:
    After writing this I found out my approach was incorrect. I have since removed the code as it was not useful.
    Dan’s team was kind enough to point me to Virgin Australia‘s site. If you inspect the source and search for hreflang, you’ll see how it should be done.
    /UPDATE
    After seeing the excellent talk by Dan Petrovic at the Melbourne WordCamp, I went to find out more about the <link rel=”alternate” hreflang=”en-AU” href=”http://somesite.com.au/” />. He has an article describing it. Essentially if you have multiple sites that have similar content (but not exactly the same) then you can instruct google that the sites serve different regions by using the rel=alternate hreflang=”en=gb” options for the link tag.
    A common scenario that you might find this useful would be in online stores where there is a different store for each country.
    eg. for site somesite.com.au you want a <link> tag like this:
    <link rel=”alternate” hreflang=”en-AU” href=”http://somesite.com.au/” />
    and for somesite.com you might want a tag like this:
    <link rel=”alternate” hreflang=”en-US” href=”http://somesite.com/” />
    and you want to remove the rel=”canonical” option too.
    I couldn’t find a plugin that would implement this in WordPress so I wrote one and put it up on github. Yoast does not have an option for this as far as I can tell.
    At some point I might put it up on wordpress.org too if it seems like there is interest in it.

  • There will be Emacs Chocolate at the #Emacs Meetup Melbourne

    I decided to make something special for the inaugural Australian emacs meetup.
    emacs chocolate
    If you want some, come to the Melbourne emacs meetup.
    Sofia’s Camberwell.
    857 Burke Road
    Camberwell 3124
    Victoria, Australia
    Friday April 26, 2013 6:00 PM

  • Launching cygwin emacs from native win32 programs

    My business’s accounting program, which I’ll call Canopus, has issues. One of which is that it assumes wordpad.exe will be in c:\windows. As far as I know, Microsoft moved wordpad.exe’s default location away from c:\windows about 20 years ago. Canopus live in hope that it will be there so installing Canopus always requires one to copy wordpad.exe to c:\windows
    The other problem with Canopus is that it does not fork when launching wordpad.exe, so you can’t continue using it until you close wordpad. This is stupid as most likely you need the information in wordpad to do further work in Canopus.
    So a long time ago I came up with a solution. With the help of a friend (well, he wrote it for me in Delphi) I developed a small program called wordpad.exe which reads a config.ini file to configure which editor to open. It makes a copy of the file in $1 and launches whatever editor you specify in the config.ini with $1 as its parameter.
    This worked well and everyone was happy until I decided to use cygwin emacs instead of the native win32 compiled emacs.
    The problem is that cygwin emacs expects paths to be of the unix flavour (/home/jason/whatever) rather than the dos flavour (c:\users\jason\whatever). And so the rabbit hole needed and extension.
    I didn’t want to have to modify my wordpad.exe program as that would require me to get the lazarus (open source delphi replacement) toolchain working again, so I decided to write a shell script wrapper to do it.
    launchemacs.sh:

    #!/bin/bash
    /usr/local/bin/emacsclient.exe -n $(cygpath "$1")

    and my config.ini for wordpad.exe:

    [Config]
    Exe=c:\cygwin\bin\run.exe
    option=/usr/bin/bash.exe "/cygdrive/c/Users/jason/launchemacs.sh"

    You can see that wordpad.exe launches the cygwin run.exe which calls the launchemacs.sh file with the first parameter.

  • Project Euler No. 2 in Emacs elisp

    My second Project Euler solution.
    Project Euler No. 2
    This was a little tougher to solve than No. 1. I decided to use recursion to solve it, and as I haven’t had to write anything that used recursion since I was at Uni, it took me a while go remember how to do it. Also, it was an elisp learning exercise.
    Things I learnt:

    1. successive statements inside an if statement need to be inside a progn.
    2. passing in the evens variable was the way to solve this without needing a static variable. Thanks to spacebat for that one. Apparently that is the functional programming way of doing things.
    ;; Project Euler no 2. Calculate the sum of all even terms in the
    ;; fibonacci sequence of numbers that do not exceed 4,000,000
    (defun fibonacci (x y)
      "calculate the next number in the fibonacci sequenece"
      (+ x y))
    (defun fibonacci-sum  (fib1 fib2 target &optional evens)
      "find the next fibonacci number bigger than target, given fib1 and fib2"
      (unless evens
        (setq evens 0))
      (let ((fib3 (fibonacci fib1 fib2 ))
            )
        (if (<=  fib3 target)
            (progn (if (zerop (mod fib3 2))
                       (setq evens (+ fib3 evens)))
                   (message (format "fib3 is: %d, evens: %d" fib3 evens))
                   (setq fib3  (fibonacci-sum fib2 fib3 target evens)))
          (format "fib3: %d > %d target, evens = %d" fib3 target evens)
          )
        )
      )
    (fibonacci-sum 0 1 4000000)
    
  • Install perl modules automatically using lib::xi

    So you’ve downloaded a really cool perl script but it has 200 module dependencies? You could just install them one by one with cpanm or, you could use lib::xi. It automatically installs modules your perl script uses.

     # to install missing libaries automatically
     $ perl -Mlib::xi script.pl
    
  • OAuth 2.0 in emacs – Part 4

    Finally managed to authenticate against WordPress.com using the oauth2 library.
    Here is my sample code which returns an access token.

    (let ((code (oauth2-request-authorization "https://public-api.wordpress.com/oauth2/authorize?blog=jasonblewis.wordpress.com" "2194" "" "" "https://emacstragic.net")
     ))
      (oauth2-request-access "https://public-api.wordpress.com/oauth2/token" "2194" "" code "https://emacstragic.net" )
      )
    

    WordPress at least seems to be fussy about what you use as the Redirect URI. It needs to match the uri you set in the app settings of the wordpress developer page or you denied access.

  • Project Euler No. 1 in Emacs elisp

    My first Project Euler solution, and my first emacs elisp program.
    Multiples of 3 and 5.
    If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
    Find the sum of all the multiples of 3 or 5 below 1000.
    My solution:

      (defun divisibleby (dividend divisor)
        "check if dividend is divisible by divisor"
        (if (not(integerp dividend))
            (error "dividend must be an integer"))
        (if (not(integerp divisor))
            (error "divisor must be an integer"))
        (zerop (mod dividend divisor))
        )
    (let  ((lower 1)
           (upper 1000)
           (sum 0))
      (loop for i from lower to (- upper 1) do (if (or (divisibleby i 3)
                                                  (divisibleby i 5))
                                             (setq sum (+ sum i)))
            )
      (message "sum is: %d" sum)
      )
    
  • emacs elisp first attempts

    For a long time I have wanted to learn elisp in emacs. I tried the various tutorials but I get bored of them too quickly. Too many words not enough action for me. I was reminded of Project Euler by Daniel Silverstone so I thought I’d have a crack at one of the problems. No. 1, Multiples of 3 and 5 seemed like a good place to start.
    Here are my first attempts and questions I came up against. Many thanks to the people of #emacs on freenode for answering my questions.
    Q: Why doesn’t the elisp doco within emacs have examples?
    A: Often it does. But if not, check the elisp info pages. info-apropos. Or use s in an info buffer to search.
    Q: is there a printf equivalent in lisp?
    A: format – at first this confused me as I thought it would just format a string and not print it. I think it was a fundamental misunderstanding of what print actually does.
    Q: in a defun, what should you do if the caller supplies an invalid parameter? like supplying a float instead of an int?
    A: (error “Fail!”), check with integerp
    (error ‘wrong-type-argument “Foo is bad, and you should feel bad.”)
    Q: how would I check if a number is divisible by another number? (to give an integer result)
    A: ijp | k-man: (defun (divisiblep x y) "is x divisible by y?" (zerop (mod x y)))
    My final hurdle was having too many brackets around an if statement. I had written

    if (or ((divisibleby i 3))

    instead of

    if (or (divisibleby i 3)

    Once I had fixed that my program worked.

  • OAuth 2.0 in emacs – Part 3

    I’m still stuck with WordPress and OAuth 2.0. I sent a support email to them but they have not followed up yet. I did find the problem described on Stack Overflow with no solution yet either.
    I thought I might try and get OAuth 2.0 working with a different service.
    Twitter does not appear to officially support OAuth 2.0 yet. Twittering mode in emacs authenticates with OAuth 1 and appears to have its own code for doing so.
    My next thought was to see if I could find a test OAuth 2.0 server to do my own testing on. I found there is a discussion and links to one on Stack Overflow, but its OAuth 1.
    Google! of course, google supports OAuth 2.0. My next steps will be to see if I can authenticate against that from emacs.
    A quick search of ELPA revealed the google-contacts package and lo and behold! it was written by Julian Danjou who wrote the OAuth 2 library I am planning to use. It looks like a really good example to follow too.
    Then I had the idea that maybe if I try the authentication against a blog hosts on wordpress.com rather than my self hosted WordPress site. And sure enough, that works. So I’m back in business. More in part 4.

  • OAuth 2.0 in emacs – Part 2

    Back the basics.
    Reading through the docs again I decided try and and authorise using this url first.
    https://public-api.wordpress.com/oauth2/authorize?client_id=2194&redirect_uri=https://emacstragic.net&response_type=code
    This takes me to a wordpress.com page to ask me to authorise.
    So, then it started to make sense to me.

    (oauth2-request-authorization "https://public-api.wordpress.com/oauth2/authorize" "2194" "" "" "https://emacstragic.net")

    That above function launches your browser on the wordpress OAuth page and asks you to authorise the app to one of your blogs. It also prompts the user in emacs to enter the authorisation code the browser displays.
    Screen Shot 2013-02-23 at 8.33.21 AM
    But selecting my blog and hitting Authrorize gives me an error You must connect your Jetpack plugin to WordPress.com to use this feature. which is strange because it is already connected.