Last active
August 29, 2015 13:55
-
-
Save grantmacken/8759829 to your computer and use it in GitHub Desktop.
twitter hashtag autolinking in query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>' | |
return replace($input, $pattern , $replacement, $flags ) | |
} | |
let $flags := 's' | |
let $input := $trim($model('page-content')/*/text()) | |
let $pattern := "(\n)" | |
let $seqLines := tokenize($input, $pattern) | |
let $lines := map(function($line) { | |
let $replaced := '<div>' || $hashTag($line) || '<br/></div>' | |
let $l := util:parse($replaced ) | |
return | |
( $l/*/node()) | |
}, $seqLines) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
xquery regex for twitter style hashtags