Tag Archive for code

Crazy Prolog Code

output(X,Y) :- my_output(X,Y)!.

my_output(X,Y) :- my_output_(X,Y);
my_output_(X,Y) :- my_output__(X,Y);
my_output__(X,Y) :- my_output___(X,Y);
my_output___(X,Y) :- write([X | [is | [Y | []]]]), my_________().
my_________() :- my________().
my________() :- my_______().
my_______() :- my______().
my______() :- my_____().
my_____() :- my____().
my____() :- my___().
my___() :- my__().
my__() :- my_().
my_() :- my().
my() :- !.

source

Crazy Perl Code

$_ = shift;
s////g;
print;

################

while(<>) {
/^(w+)/;
print $_.',';
}

#############

if(($_ = 5)==5) {print;}

##############

$x = 7;
if (($x <=> 0)==($x=$x)) {print 'true';}
else{print 'false';}

#######################

print "Hello World" or die "trying";

######################

print eval qw(qw(qw()));

source

Crazy C++ Code

char *s = "DEHLORW";
char *d = "2133464530";

for(i=0; d[i]; i++)
cout << s[d[i]-'0'];

///////////////////////////

i = 1<<1;
i |= 1;
cout << (i<<2) << ":" << (i>>2) << (i==2);

/////////////

x = 0;
cout << (x==0) << (x=0) << (0);

////////////////////////////

class c{
c(int i=0);
public: int a;
};

c::c(int i): a(i) {}

source

Disablig deprecated HTML code

font,basefont {
color:inherit; /* Standard browsers */
color:expression(this.parentNode.currentStyle['color']); /* IE */
font:inherit; /* Standard browsers. Font instead of font-size for Opera */
font-family:expression(this.parentNode.currentStyle['fontFamily']); /* IE */
font-size:100%; /* All browsers. Sizes are inherited */
}
center {
text-align:inherit; /* Standard browsers */
text-align:expression(this.parentNode.currentStyle['textAlign']); /* IE */
}
s,strike,u {
text-decoration:inherit; /* Standard browsers */
text-decoration:expression(this.parentNode.currentStyle['textDecoration']); /* IE */
}
*[align] { text-align:inherit; }  /* Standard browsers */
* { text-align:expression(this.align ? this.parentNode.currentStyle['textAlign'] : ''); }  /* IE */
img { margin:0; border:none; }  /* All browsers. Borders & margins are not inherited */
ol { list-style-type:decimal; }  /* All browsers. Removes the type attribute */
body { background-color:transparent; /* All browsers */ }
table,tr,th,td {
width:auto; /* All browsers */
height:auto; /* All browsers */
background-color:transparent; /* All browsers */
vertical-align:inherit; /* All browsers (works in IE) */
border:none; /* All browsers. Borders are not inherited */
}

source

Code Cache Prevention Code

<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="expires" content="FRI, 13 APR 1999 01:00:00 GMT">

source

Unobtrusive Code Highlighter By Dan Webb

/* Unobtrustive Code Highlighter By Dan Webb 11/2005 Version: 0.4 */

var CodeHighlighter = { styleSets : new Array };

CodeHighlighter.addStyle = function(name, rules) {
if ([].push) this.styleSets.push({
name : name,
rules : rules,
ignoreCase : arguments[2] || false
})

function setEvent() {
var old = window.onload;

if (typeof window.onload != 'function') {
window.onload = function() { CodeHighlighter.init() }
} else {
window.onload = function() {
oldonload();
CodeHighlighter.init();
}
}
}

if (this.styleSets.length==1) setEvent();
}

CodeHighlighter.init = function() {
if (!document.getElementsByTagName) return;
if ("a".replace(/a/, function() {return "b"}) != "b") return;

var codeEls = document.getElementsByTagName("CODE");
codeEls.filter = function(f) {
var a =  new Array;
for (var i = 0; i < this.length; i++) if (f(this[i])) a[a.length] = this[i];
return a;
}

var rules = new Array;
rules.toString = function() {
var exps = new Array;
for (var i = 0; i < this.length; i++) exps.push(this[i].exp);
return exps.join("|");
}

function addRule(className, rule) {
var exp = (typeof rule.exp != "string")?String(rule.exp).substr(1, String(rule.exp).length-2):rule.exp;
rules.push({
className : className,
exp : "(" + exp + ")",
length : (exp.match(/(^|[^])([^?]/g) || "").length + 1,
replacement : rule.replacement || null
});
}

function parse(text, ignoreCase) {
return text.replace(new RegExp(rules, (ignoreCase)?"gi":"g"), function() {
var i = 0, j = 1, rule;
while (rule = rules[i++]) {
if (arguments[j]) {
if (!rule.replacement) return "<span class="" + rule.className + "">" + arguments[0] + "</span>";
else {
var str = rule.replacement.replace("$0", rule.className);
for (var k = 1; k <= rule.length - 1; k++) str = str.replace("$" + k, arguments[j + k]);
return str;
}
} else j+= rule.length;
}
});
}

function highlightCode(styleSet) {
var parsed;
rules.length = 0;

var stylableEls = codeEls.filter(function(item) {return (item.className.indexOf(styleSet.name)>=0)});

for (var className in styleSet.rules) addRule(className, styleSet.rules[className]);

for (var i = 0; i < stylableEls.length; i++) {
if (/MSIE/.test(navigator.appVersion) && stylableEls[i].parentNode.nodeName == 'PRE') {
stylableEls[i] = stylableEls[i].parentNode;

parsed = stylableEls[i].innerHTML.replace(/(<code[^>]*>)([^<]*)</code>/i, function() {
return arguments[1] + parse(arguments[2], styleSet.ignoreCase) + "</code>"
});
parsed = parsed.replace(/
( *)/g, function() {
var spaces = "";
for (var i = 0; i < arguments[1].length; i++) spaces+= "&nbsp;";
return "
" + spaces;
});
parsed = parsed.replace(/	/g, "&nbsp;&nbsp;&nbsp;&nbsp;");
parsed = parsed.replace(/
(</w+>)?/g, "<br />$1").replace(/<br />[

s]*<br />/g, "<p><br></p>");

} else parsed = parse(stylableEls[i].innerHTML, styleSet.ignoreCase);

stylableEls[i].innerHTML = parsed;
}
}

for (var i in this.styleSets) highlightCode(this.styleSets[i]);
}

CodeHighlighter.addStyle("css", {
ccomment : {
exp  : //*[^*]**+([^/][^*]**+)*//
},
keyword : {
exp  : /@w[ws]*/
},
selector : {
exp  : "([w-:[.#][^{};>]*)(?={)"
},
property : {
exp  : "([w-]+)(?=s*:)"
},
unit : {
exp  : /([0-9])(em|en|px|%|pt)/,
replacement : "$1<span class="$0">$2</span>"
},
url : {
exp  : /url([^)]*)/
}
});

CodeHighlighter.addStyle("html", {
ccomment : {
exp: /&lt;!s*(--([^-]|[
]|-[^-])*--s*)&gt;/
},
tag : {
exp: /(&lt;/?)([a-zA-Z]+s?)/,
replacement: "$1<span class="$0">$2</span>"
},
string : {
exp  : /'[^']*'|"[^"]*"/
},
attribute : {
exp: /([a-zA-Z-:]+)(=)/,
replacement: "<span class="$0">$1</span>$2"
},
doctype : {
exp: /&lt;!DOCTYPE([^&]|&[^g]|&g[^t])*&gt;/
}
});

CodeHighlighter.addStyle("javascript",{
comment : {
exp  : /(//[^
]*
)|(/*[^*]**+([^/][^*]**+)*/)/
},
brackets : {
exp  : /(|)/
},
string : {
exp  : /'[^']*'|"[^"]*"/
},
keywords : {
exp  : /(arguments|break|case|continue|default|delete|do|else|false|for|function|if|in|instanceof|new|null|return|switch|this|true|typeof|var|void|while|with)/
},
global : {
exp  : /(toString|valueOf|window|element|prototype|constructor|document|escape|unescape|parseInt|parseFloat|setTimeout|clearTimeout|setInterval|clearInterval|NaN|isNaN|Infinity)/
}
});

source

search and replace across multiple files with Perl

#print the result of search-and-replace to the terminal
perl -pe 's/bart/milhouse/g' test.html

#search-and-replace, with backup
#leave the suffix off of -i to overwrite
perl -i.bak -pe 's/bart/milhouse/g' test.html

#echo the number of lines in a file
perl -lne 'END { print $t } @w = /(w+)/g; $t += @w' test.html

#cat file with line numbers
# -p prints $_ each iteration
perl -pe '$_ = "$. = $_"' test.html

# recursive search-and-replace, only on shells that support file globs
perl -i.bak -pe 's{bart}{milhouse}' **/*html

source

Wrap tab-delimited strings in HTML

#! /usr/bin/perl -w
use strict;

#Time-stamp: <2005-01-12 11:30:05 NSussman>

############################################################
#                                                          #
#                                                          #
#                                                          #
#                      NOAH SUSSMAN                        #
#                                                          #
#                  Tabs To Targets                         #
#                                                          #
#                Created Jan 12 2005 at 1:10am             #
#                                                          #
#   Given tab-delimited data, place each line into a       #
#   heredoc and print to STDOUT then go on to the          #
#   next line                                              #
#                                                          #
#   Ignore #commented lines and blank lines.               #
#                                                          #
#                                                          #
############################################################

my $input_file= shift;    #file containing tab-delimited data

open(TABBED_DATA_HANDLE, $input_file) || die("Couldn't open $input_file
");

chomp(my @whole_file=<TABBED_DATA_HANDLE>);

foreach my $one_line (@whole_file) {
next if ($one_line =~ m/^s*$/);    #ignore blank lines
next if ($one_line =~ m/^#/);       #ignore lines starting with "#"

my @record = split(/	/, $one_line);

print "
";

#Place targets in the heredoc.
print <<CODEFRAGMENT;
<!--////////////////$record[2] block of html////////////////////////////////////////////////////////////////-->

<tr>
<td class="smelly">$record[1]</td>
<td class="dogfood">$record[0]</td>
<td class="teaParty">$record[2]</td>
</tr>

<!--////////////////end of $record[2] block of html////////////////////////////////////////////////////////////////-->
CODEFRAGMENT
#end of heredoc.

print "
";
}

=item
Here is an example data file:

big	fuzzy	bear
little	orange	kangaroo
large	grumpy	koala
small	sleepy	panda

=cut

source

Why SEO is Important

Page-specific
Unique title tag
Unique meta description (Yahoo is the only one using meta keywords anymore)
One h1 tag (the text used in your h2-h4 tags is still very important, though)
Site-wide
Allow only quality outbound links (eg. rel="nofollow" on all user-generated links)
Block duplicate content from being indexed with robots.txt
Structure your CSS so the content can go first
Consistent navigation that allows bots to get to any page on your site within 2 or 3 clicks

source

BASE tag insertion macro

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Insert a BASE HREF tag just after the head tag of an HTML document
;; Positions point just after the second forwardslash in "HTTP://"

(fset 'insert-base-href
[?M-< ?C-s ?< ?h ?e ?a ?d ?C-e return return return up ?< ?b ?a ?s ?e ?  ?h ?r ?e ?f ?= ?" ?h ?t ?t ?p ?: ?/ ?/ ?" ?> left left])

source