Skip to content

Instantly share code, notes, and snippets.

View juji's full-sized avatar
💭
feeling awesome

Tri Rahmat Gunadi juji

💭
feeling awesome
View GitHub Profile
@juji
juji / pandoc-svg.py
Created July 22, 2017 19:53 — forked from jeromerobert/pandoc-svg.py
Pandoc filter to create PDF files from SVG
#! /usr/bin/env python
"""
Pandoc filter to convert svg files to pdf as suggested at:
https://github.com/jgm/pandoc/issues/265#issuecomment-27317316
"""
__author__ = "Jerome Robert"
import mimetypes
import subprocess
@juji
juji / server.sh
Last active May 31, 2017 06:37
bash 5 line server
#!/usr/bin/env bash
# usage:
# ./server.sh [port] [response]
#
RESPONSE="HTTP/1.1 200 OK\r\nConnection: keep-alive\r\n\r\n${2:-"OK"}\r\n"
while { echo -en "$RESPONSE"; } | nc -q 0 -l -p "${1:-8080}"; do
echo "================================================"
echo "you can do stuff on request"
@juji
juji / cleanUTF8.js
Last active April 21, 2017 07:43
clean non utf8 character
function cleanUTF8(str){
return str.replace(/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]|[\x00-\x7F][\x80-\xBF]+|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/g,'')
.replace(/\xE0[\x80-\x9F][\x80-\xBF]|\xED[\xA0-\xBF][\x80-\xBF]/g,'');
}
@juji
juji / getDir.bash
Last active April 7, 2016 06:51
Bash get script directory
SOURCE="$0"
while [ -h "$SOURCE" ]; do
# resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
cd "$DIR"
@juji
juji / regexReplace.js
Last active December 10, 2015 05:22
Replace regex special character
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
function toRegex(str,flag){
return new RegExp(escapeRegExp(str),flag);
}
@juji
juji / validateEmail.js
Last active August 29, 2015 14:02
JS - validate email
function validEmail(email){
return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email);
}
(function addXhrProgressEvent($) {
var originalXhr = $.ajaxSettings.xhr;
$.ajaxSetup({
progress: function() { console.log("standard progress callback"); },
xhr: function() {
var req = originalXhr(), that = this;
if (req) {
if (typeof req.addEventListener == "function") {
req.addEventListener("progress", function(evt) {
that.progress(evt);
var cleanChar = function(str){
var f = [
String.fromCharCode(8220), //“
String.fromCharCode(8221), //”
String.fromCharCode(8216), //‘
String.fromCharCode(8217), //‘
String.fromCharCode(8211), //–
String.fromCharCode(8212), //—
String.fromCharCode(189), //½
String.fromCharCode(188), //¼
@juji
juji / cleanUTF.php
Last active August 29, 2015 14:02
PHP - clean unicode strings
function cleanUTF($some_string){
//reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ?
$some_string = preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'.
'|[\x00-\x7F][\x80-\xBF]+'.
'|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'.
'|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'.
'|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
'', $some_string );
//reject overly long 3 byte sequences and UTF-16 surrogates and replace with ?
@juji
juji / errorControl.php
Last active August 29, 2015 14:02
PHP - error reporting control
error_reporting(E_ALL | E_STRICT);
// handle all error
ob_start(function($out){
$e = error_get_last();
if(!empty($e)) {
return $e['message'] . ' ( ' . $e['file'] . ' [' . $e['line'] . '] )';
}
return $out;
});