Skip to content

Instantly share code, notes, and snippets.

View grantmacken's full-sized avatar
🏠
Working from home

Grant MacKenzie grantmacken

🏠
Working from home
View GitHub Profile
@grantmacken
grantmacken / hashtag.xq
Last active August 29, 2015 13:55
twitter hashtag autolinking in query
xquery version "3.0";
let $trim := function($arg){
replace(replace($arg,'\s+$',''),'^\s+','')
}
let $hashTag := function( $input ){
let $flags := ''
let $pattern := "(^|\s)((#)([A-Za-z]+[A-Za-z0-9_]{1,15}))(\s|$)"
let $replacement := '<span>$1<a href="/tag/$3">$4</a>$5</span>'
@grantmacken
grantmacken / md2-pygments.py
Last active August 29, 2015 13:56
Markdown2 fenced blocks and Pygments. Out of the I box I just couldn't get it work. So I don't use fenced blocks and hacked my own solution.
from lxml.html import tostring, fromstring, html5parser
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import guess_lexer, get_lexer_by_name
from pygments.filters import VisibleWhitespaceFilter
# hack hack ...
# ignore code contained in pre. I just want the text in the pre
def flatten(elem, include_tail=0):
text = elem.text or ""
for e in elem:
@grantmacken
grantmacken / post-to-twitter.py
Last active August 29, 2015 13:56
post to twitter from a markdown file
# -*- coding: utf-8 -*-
import re
import sys
import datetime
import math
import codecs
import argparse
import fileinput
import markdown2
@grantmacken
grantmacken / group-by-date.xqm
Last active August 29, 2015 13:56
Xquery 3 function that uses 'group by' to list entries by year month day
declare
function post:archive-feed($node as node(), $model as map(*)) {
let $itemCount := function( $seq ){
if( count( $seq ) eq 1 )
then( string(count( $seq )) || ' post')
else( string(count( $seq )) || ' posts') }
let $getMonth := function( $month ){
('January', 'February', 'March', 'April', 'May', 'June','July', 'August', '
September', 'October', 'November', 'December')[$month]
@grantmacken
grantmacken / hashURL.xq
Last active August 29, 2015 13:57
Generate a base64 md5 hash from a URL, which can become a 'unique' URL safe resource or file name
xquery version "3.0";
import module namespace util="http://exist-db.org/xquery/util";
let $href := "${url}"
let $base64flag := true()
let $alogo := 'md5'
let $hash := replace(util:hash($href, $alogo, $base64flag), '(=+$)', '')
return
translate( $hash, '+/', '-_')
@grantmacken
grantmacken / tests.xqm
Created May 2, 2014 00:28
using map in tests to check if node has tag-name
declare
%test:args('<a class="h-entry" href="http://microformats.org/2012/06/25/microformats-org-at-7">microformats.org at 7</a>')
%test:assertExists
%test:assertTrue
%test:assertXPath("count($result) = 1")
%test:assertXPath("local-name($result[1]) eq 'entry'")
%test:assertXPath(" 'name' = ( map(function($n) { local-name($n) }, $result[1]/*) ) ")
%test:assertXPath(" 'url' = ( map(function($n) { local-name($n) }, $result[1]/*) ) ")
function st:just-a-hyperlink($node as element()) as element() {
mf2:dispatch($node)
@grantmacken
grantmacken / test-runner.xq
Last active August 29, 2015 14:02
LIVE browser preview testing of tests found in the tests collection as we are working on a xquery library module
xquery version "3.0";
(:~
We want LIVE browser preview testing of tests found in the tests collection
when we are working on an xquery library module
gistID: 61082e441e43653b8b75
https://gist.github.com/grantmacken/61082e441e43653b8b75
I use this with a Komodo toolbox macro which on save
uploads working file to existdb localhost server
@grantmacken
grantmacken / reposCreateNewRepo.sh
Created December 19, 2014 01:29
a bash function I will use to create a repo in current folder ussing github api
#!/bin/bash +x
function reposCreateNewRepo(){
local apiUrlBase='https://api.github.com'
GITHUB_ACCESS_TOKEN=$( cat ${GITHUB_ACCESS_TOKEN_LOCATION} )
PROJECT_NAME=$( echo ${PWD##*/} )
PROJECT_DESCRIPTION="website project ${PROJECT_NAME}"
local chkGitStatus=$( git status 2>/dev/null )
echo "CHECK! if ${PROJECT_NAME} under git control"
if [[ -n ${chkGitStatus} ]] ; then
@grantmacken
grantmacken / controller.xql
Created February 9, 2015 17:51
controller.xql
xquery version "3.0";
declare variable $exist:path external;
declare variable $exist:resource external;
declare variable $exist:controller external;
declare variable $exist:prefix external;
declare variable $exist:root external;
import module namespace xmldb="http://exist-db.org/xquery/xmldb";
@grantmacken
grantmacken / styles.mk
Last active October 6, 2015 23:20
Make function get mime-type using #existdb mime-types file
# makes a shell call to node and uses cheerio to extract mimetype from
# passed in file name
# depends on cheerio node_module in reachable node path
getMimeType = $(shell node -pe "\
fs = require('fs');\
re = /$1/;\
n = require('cheerio').load(fs.readFileSync('$(EXIST_HOME)/mime-types.xml'),\
{ normalizeWhitespace: true, xmlMode: true});\
n('extensions').filter(function(i, el){\
return re.test(n(this).text());\