prettify

baseline

Table based infobox with css3

Table based infobox: Infotable html:
<table><thead><tr>
<th>Attribute</th><th>Value</th>
</tr></thead><tbody>
<tr><td>width</td><td>100px</td></tr>
<tr><td>title</td><td>text</td></tr>
<tr><td>colspan</td><td>4</td></tr>
<tr><td>rowspan</td><td>2</td></tr>
</tbody></table>

<table><thead><tr><th>
 Title
</th></tr></thead><tbody><tr><td>
<ul>
  <li>First element</li>
  <li>Second element</li>
  <li>Third element</li>
  <li>Fourth element</li>
  </ul>
</td></tr></tbody></table>

css (part of Reset.css included):
* { /*** Reset start ***/
 padding:0;
 margin:0;
 font-family:Tahoma,Verdana,Arial;
 font-size:100%;
}
ul {
 list-style:disc outside;padding-left:25px;
}
table {
 border-spacing:0;
 border:1px solid #666;
}
body {color:#333;}
/* Reset end */
table {
 margin:20px;
 width:10em;
 
 background:#ace;
 background: -moz-linear-gradient(top,#eff,#ace 7%, #ace 90%,#777 );
 background: -webkit-gradient(linear, left top, left bottom,  color-stop(0,#eff), color-stop(0.07, #ace), color-stop(1,#777));

 -moz-border-radius:.6em;
 border-radius:.6em;
 
 -webkit-box-shadow: .4em .4em .4em rgba(0,0,0,0.5);
 -moz-box-shadow: .4em .4em .4em rgba(0,0,0,0.5);
 box-shadow: .4em .4em .4em rgba(0,0,0,0.5);
}
 
table th {
 padding:.4em;
 text-align:center;
 border-bottom:1px #888 solid;
 background-color:transparent;
 text-shadow: .06em .06em .1em rgba(0,0,0,0.5);
}
table td {
 background:#fafafa;
 padding:.5em;
 border:1px #888 solid;
}
table tr:last-child td:first-child {
 -moz-border-radius-bottomleft:.5em;
 border-bottom-left-radius:.5em;
}
table tr:last-child td:last-child {
 -moz-border-radius-bottomright:.5em;
 border-bottom-right-radius:.5em;
}

Reset.css

My own Reset.css
Actually it contains Base.css as well. I don't see the reason for an extra Base.css
I don't test on IE7 (forget about IE6).
* {
 -moz-box-sizing:border-box;
}
/*upd: 20111020 ************************************/
* {
 box-sizing:border-box;
}
html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
strike, strong, sub, sup,
dl, dt, dd,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
 margin:0;
 padding:0;
 border:0;
 font-weight:inherit;
 font-size:100%;
 font-style:inherit;
 font-family:inherit;
 border:1px none #000;
 background:transparent;
 vertical-align:baseline;
}
body {
 line-height: 1.2;
 color:#333;
}
body, select, input, textarea {
 font-family:Tahoma,Verdana,Arial,helvetica, Sans-Serif;
 margin:0;
}
article,aside,canvas,details,figcaption,figure,
footer,header,hgroup,menu,nav,section,summary { 
 display:block;
}
pre,code {
 font-family:Monospace;
}
html,body {
 height:100%;
}
table {
 border-collapse:collapse;
 border-spacing:0;
 /*width:100%;*/
}
th,td {
 border:1px solid #666;
 /*padding:.5em;*/
 vertical-align:top; 
}
caption {
 margin-bottom:.5em;
 text-align:center;
}
blockquote,q { quotes: "\201C" "\201D"; } 
blockquote:before { content: "\201C"; font-weight: bold; }  
blockquote:after { content: "\201D"; font-weight: bold; }

ol,ul {list-style-position:outside;padding-left:25px;}
ol {list-style-type:decimal;}
ul {list-style-type:disc;}
li:before {display:marker;marker-offset:1em;}

ins {text-decoration:underline;}
del {text-decoration: line-through;}
em {font-style:italic;}
h1 {font-size:200%;}
h2 {font-size:178%;}
h3 {font-size:159%;}
h4 {font-size:141%;}
h5 {font-size:126%;}
h6 {font-size:112%;}
h1,h2,h3,h4,h5,h6 {margin:1em 0;}
h1,h2,h3,h4,h5,h6,strong,th {font-weight:bold;}
mark {background-color:#ff9;}

abbr,acronym {
 border-bottom:1px dotted #000;
 cursor:help;
}
p,fieldset {margin-bottom:1em;} 
Credits:
Yahoo yui3 cssreset
Eric Meyer
HTML5 Reset.css
http://html5boilerplate.com/

Table Layout for web pages

You should never use tables for layout. You should use div. Having said that and having spent some time trying to force div to look like table I decided that enough is enough. If a table is what I need, a table is what I will use. What I need is a layout:
  • Header should cover the top of the window.
  • Header height should be determined by its contents.
  • Footer should cover the bottom of the window and resize by it. It must always be at the bottom of the window except when the contents of the page are more than that.
  • Left aside width should resize according to contents. The height should resize to full window or to contents (the largest of the two)
  • Right aside should behave like left aside.
The entire think should behave as a TABLE.
<table  id="tabLayout" role="presentation">
<tbody><tr id="trHeader">
  <td colspan="3">header</td>
</tr><tr>
  <td id="tdNav">nav</td>
  <td>main</td>
  <td id="tdAside">aside</td>
</tr><tr id="trFooter">
  <td colspan="3">footer</td>
</tr></tbody></table>
/*=== Reset start ====*/
html,body,table,tr,td {
 margin:0;
 padding:0;
 -moz-box-sizing:border-box;
 -o-box-sizing:border-box;
 -webkit-box-sizing:border-box;
 box-sizing:border-box;
}
table {
 border-collapse:collapse;
 width:100%;
}
html,body {
 height:100%;
}
/*=== Reset end ===*/
table#tabLayout {
 border-collapse:collapse;
 width:100%;
 height:100%;
}
#trHeader, #trFooter {
 height:1px;
}
#tdNav, #tdAside {
 width:1px;
}

Public key cryptography with GnuPG

Problem:

I want to send a document to a partner and be sure that no one else is going to read it.

I want to sign a document and send the signarure to a partner in a way he is sure that I signed it and he can prove it if need arises.

Solution:

Public key cryptography
key
Each person involved in the secure information exchange has two keys. One private key and one public key.
Both keys are just data (text) created by an application based on some information about the person (Name, email, passphrase).
Public key
A data file that you send to anybody asking. The public key can be used to
  • encrypt a document that can be decrypted only by your private key.
  • decrypt a document that was encrypted by your private key
To make the task easier there are key servers like hkp://keys.gnupg.net where you can store you public key so that anybody can find it without bothering you.
You can go to http://keys.gnupg.net/ and search for your friends (by name or email) to find if they have public keys stored there.
Private key
A data file that only you have. You can use it to:
  • Decrypt a document that was encrypted by your public key
  • Encrypt a document that can be decrypted only with your public key. Obviously anybody can decrypt the document but that way any receiptient is sure that the file was encrypted by you.
Digital Signature
A data file containing the ID (hash) of the document to be signed plus your ID (certificate),  encrypted by your private key.
Anyone can confirm that you signed the actual document (and you can not deny it) provided he has:
  • The actual document
  • The signature document
  • Your public key
Signed document
A document that is merged with your signature.
Digital Certificate
How can I be sure that the public key and the corresponding e-mail that I have is really yours?
If it is not then I will happily encrypt the document and send it to another person who can read it. Then I will find that all my nude pictures are on 4chan for everyone to enjoy (really doubt it).
The problem can be solved using certificates. Certificates are supplied by certificate servers that can confirm your identity matches your public key.
The open source solution is the certificates supported by a web of trust

Acquire a public key and a private key

  1. Go to GnuPG for Windows and download Gpg4win
  2. Install it
  3. Run GPA (GNU Privacy Assistant). It will prompt you for a new key. Go for it.
  4. It will ask for your real name and email. These are needed
    • So that is known who is signing what
    • To be registered in the public database hkp://keys.gnupg.net so that people can search and find your public key
    So be honest and give your real name and mail.
  5. It will ask for a passphrase. This is important. It is like a password but you are not supposed to change it now and then.
    So find a small phrase you will remember and intersperse with numbers or symbols (in a way you will remember).

Encrypt a document (to send to a partner).

  1. Find your partner's public key. You can ask him (better) or look at a server.
  2. Open GPA
  3. Import your partner's public key if you have not done already so. Keys⇒Import
  4. GPA⇒Toolbar⇒Files. File Manager application starts.
  5. Select the file and Encrypt it.

Sign a document

You want to send a document that there is no reason to be encrypted. The receiving party must have a way to prove that you sent the document, and it is not changed in any way.

  1. Run GPA
  2. GPA⇒Toolbar⇒Files. File Manager application starts.
Now in File Manager:
  1. File⇒Open. Select the file you want to sign.
There are a lot of stuff you can do, but most of them are trivial if you came this far:
  • Encrypt a file using a partner's public key.
  • Encrypt a file using your own public key (only you can decrypt it). Symmetric cryptography is a lot better for the task but you can do it if you insist.
  • Sign a file using your own private key.
  • Sign and encrypt a document using your own private key
  • Decrypt a document you received using your own private key
  • Verify the signature of a document you received using your partner's public key
If you don't want to use encrypted attachments for your encrypted documents you can
  • Set your mail agent format to "text only"⇒ Copy⇒Clipboard Encrypt⇒ Paste Encrypted to your mail program⇒send mail
  • If your mail agent is supported (Outlook, outlook express, thunderbird etc) then just install the addin and you can do it through your agent.
References

Hungarian Notation

Not really Hungarian. This is how I prefix variables, Database Table columns and how I abbreviate common words in my apps (this is an Abr).
  • prefixes are application oriented. No language specific datatypes (int32, word etc)
  • prefixes are the same cross language. Each programming language used in a project should have the same name for the same variable (eg SQL, C#, Javascript)
Prefixes for variables (& Table Columns)
prefixData TypeExampleUsageComments
n numeric nPerAge Person's age Integer values. Can be negative.
sstringsPerLNmPerson's Last Name
ccurrencycPerSalaryPerson's Salary
rreal numberrPerWeightPerson's Weightused to be “f” but I needed f for functions.
ddatedPerDobPerson's DateOfBirth
ttimetMetStartMeeting's Start Time
oobjectoPersonPerson oPerson=new Person(); (C#)
var oPerson=new Person(); (C#)
ffunctionfCallBackvar fCallback=function(){};
eenumeration (string)ePerMaritalStatusPerson's Marital Statuscan have one of prespecified values: see en
enenumeration listenMaritalStatusThe structure containing the enumeration valuesvar enMaritalStatus=[SINGLE,MARRIED,SEPARATED,DIVORCED]
pPercentagepVatValue added Tax (percentage)pVat = 0.21; (=21%). Some people think p should be
reserved for pointers.
I think it is reasonable if there is a possibility to use pointers.
bBooleanbPerLicensedbPerLicensed = true; (C#, Javascript)
bPerLicensed = 1; (SQL)
nkprimary keynkPer
nfforeign keynfPerOrgPerson's organization
rvrowversionrvPermulti user updates
ArrayValuesvar Values=[];Just use the plural form of a noun.
Form element name prefixes
prefixData TypeExampleUsageComments
frmFormfrmPerEdtPerson Edit form
chkcheckboxchkbPerActivePerson's active flag
cbocombocboeMaritalStatus
btnbuttonbtnDone
fraframe-fieldsetfraAdrGroup fields for Person's address
optoptopteMaritalMarriedMarital Status implemented with option button instead of combo
txttextboxtxtsPerLNmPerson's Last Name
tmrtimer
dlgdialogdlgConfirmModal Dialog
lbllabellblsPerLNm"Last Name"
rptreportrptDailyOrders
imgimageimgLogo
 Abbreviations
Full TextAbbreviation
AddressAdr
AgentAgn
AmountAmn
CategoryCat
CityCty
CustomerCus
Date Of BirthDob
FatherFth
FirstNameFNm
Last NameLNm
IssueIss
MajorMjr
MinorMnr
MotherMth
NameNm
OrganizationOrg
PersonPer
ProductPrd
ProjectPrj
TransactionTrn

Favicons in Web Applications, Web Sites, Blogger

  1. Create a 16x16 png image using paint.net. If you want you can have transparent background.
  2. Convert to ico (you can use Visual Studio or DynamicDrive
  3. <link href='http://www.example.com/myFavicon.ico' rel='shortcut icon' type='image/icon'/>
Convert png to ico using Visual Studio (use DynamicDrive, is easier)
  1. File⇒ Open⇒ File⇒ Select favicon.png
  2. favicon.png is selected⇒ Edit⇒ Copy
  3. favicon.png is selected⇒ File⇒ New⇒ File⇒Installed Templates=General⇒Icon File⇒Open
  4. Right click⇒ Delete image type on all image types (left column)
  5. Image⇒ New Image Type⇒ 16x16x24
  6. Edit⇒ Paste
  7. File⇒ SaveAs
Now we have two favicons.
  1. favicon.png
  2. favicon.ico
Install favicon to a web site (or application)
<!-- cross browser 16x16, 24bit color (optional +32x32, 8bit color) -->
<link rel="shortcut icon" href="http://www.example.com/myicon.ico" />
<!-- Apple -->
<link rel="apple-touch-icon" sizes="57x57" href="touch-icon-iphone.png" />
<link rel="apple-touch-icon" sizes="72x72" href="touch-icon-ipad.png" />
<link rel="apple-touch-icon" sizes="114x114" href="touch-icon-iphone4.png" />
<!-- Without Apple's shadow, round corners, reflective shine -->
<link rel="apple-touch-icon-precomposed" sizes="57x57" href="touch-icon-iphone.png" />
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="touch-icon-ipad.png" />
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="touch-icon-iphone4.png" />
To change favicon just give the new one a different name.
UPDATE: DO NOT copy favicon.ico to http://www.example.com/favicon.ico
If you do, you will not be able to change favicon, because browsers have no way to know that the new favicion is different from the one they have chached.
 
Install favicon to Blogger
Jun 2011 UPDATE:
Blogger now supports favicons:
Blogger⇒ Dashboard⇒ Design⇒ Favicon⇒ Edit⇒ Choose File⇒ Select your ico from your computer⇒ SAVE

Old Version:

All browsers support .ico favicons. IE has issues with .png favicons. Blogger does not accept .ico files. So:
  • If you have another site to upload favicon.ico
    (I used http://sites.google.com/site/myname. Go http://www.google.com/sites to claim your site).:
    1. Upload favicon.ico
    2. CODE to insert:
      <link href='http://sites.google.com/site/myname/favicon.ico' rel='shortcut icon' type='image/icon'/>
  • Otherwise (ONLY if you don't have a site and cannot get one in http://www.google.com/sites)
    1. Upload favicon.png as image to blogger. Copy the link.
    2. CODE to insert:
      <link href='http://BloggerLink/favicon.png' rel='shortcut icon' type='image/png'/>
Now you have the CODE to insert to Blogger template:
  • Blogger⇒ Dashboard⇒ Design⇒ Edit HTML⇒ Ctrl-F (find): </head>
  • Just before </head> insert the CODE you found on the previous step;

View your blog. Your favicon should be displayed instead of Google's.

Model View Controller, Javascript

How to write Model View Controller (MVC Code) for the form:

Problem:

Write Model View Controller (MVC) code without any framework:

Solution using just javascript.

Copy SQL Server Diagram

Problem:

A team is developing an application. Each developer works on a copy of the DB (as should)

When the DB Schema is changed a script is used to roll-out the changes.

But what about the Database Diagram? How can each developer have an updated Schema?

Solution

USE DATABASE_NAME
Replace 'Diagram_Name' with the name of the diagram we want to copy.
Query => Results To => Results to text
Run the script:
DECLARE @line NVARCHAR(MAX)=''
,@index INT=1
,@chunk INT=32
,@diagram_id INT=(SELECT diagram_id FROM sysdiagrams WHERE name='Diagram_Name')
,@newId INT=0;
DECLARE @size INT=(SELECT DATALENGTH(definition) FROM sysdiagrams WHERE diagram_id=@diagram_id);

SELECT @line = 'INSERT INTO sysdiagrams ([name], [principal_id], [version], [definition])'
            + ' VALUES (''' + [name] + ''', '+ CAST (principal_id AS VARCHAR(100))+', '+CAST (version AS VARCHAR(100))+', 0x);'
                    FROM sysdiagrams WHERE diagram_id = @diagram_id
PRINT 'SET NOCOUNT ON'
PRINT 'DECLARE @id INT;'
PRINT @line
PRINT 'SET @id=SCOPE_IDENTITY();'
WHILE @index < @size BEGIN
 SELECT @line =  
  'UPDATE sysdiagrams SET [definition] .Write ('
  + ' ' + UPPER(sys.fn_varbintohexstr (SUBSTRING (definition, @index, @chunk)))
  + ', null, 0) WHERE diagram_id = @id;' -- index:' + CAST(@index AS VARCHAR(100))
 FROM    sysdiagrams 
 WHERE    diagram_id = @diagram_id
 PRINT @line
 SET @index = @index + @chunk
END
  • In the results windows an UPDATE_SCRIPT is output that can recreate the Database Diagram.
  • Send the UPDATE_SCRIPT to target computer.
  • Open SQL Server Management Studio.
  • New Query
  • Select target Database
  • If there is no diagram click on Database_Diagrams and confirm to let Management Studio create sysdiagrams table.
  • If an older version of the diagram exists, delete it.
  • Paste UPDATE_SCRIPT
  • RUN UPDATE_SCRIPT
  • Refresh Database to see the created Diagram.
  • You are done.
Credits: http://www.conceptdevelopment.net/Database/ScriptDiagram2008/

Blogger code prettifier

Problem:

When posting code in blogger looks awful.

Solution:

There is a nice open source project that prettifies the posted code.

Download code

  • http://code.google.com/p/google-code-prettify/downloads/list
  • download prettify-small-21-Jul-2010.zip (or latest) (I includee the date so I remember when to update)
  • Extract to any folder you like on your disk.
  • open folder "prettify" and leave it open (will be needed soon)
  • Use Notepad++ for opening and copying minimized file content. Most editors mess long lines.

Create a Gadget in Blogger:

  • http://www.blogger.com
  • Layout
  • Add a Gadget (anywhere)
  • Basics
  • HTML/Javascript
  • Title: prettify (choose any title you like)
  • Content:
<style type="text/css">
===>insert contents of  prettify.css <===
</style>

<script type="text/javascript">
 ===> insert contents of prettify.js (do not use minified-does NOT work!) <===
</script>
I customize adding:
(Seems that blogger encloses post in a div with class="post" so that we can style it safely without changing the rest of the page.)
<style type="text/css">
.post .prettyprint { 
 border:1px dotted #666; 
 background-color:#ffc; 
 overflow:auto; 
}
.post .prettyprint ol.linenums li {
 list-style-type:decimal-leading-zero;
}
.post .nocode { 
 background-color:#fff; 
 border-right:1px dotted #666; 
} 
</style>

<script type="text/javascript>
 ===> insert contents of lang-css.js <===
 ===> insert contents of lang-sql.js <===
 ===> if needed insert contents of lang-*.js where * is apollo,hs,lisp,lua,ml,proto,vb,vhdl,wiki... <===
</script>

Modify Blogger Template:

  • Layout
  • Edit HTML
  • Click in template TextArea
  • find (^F) <body>
  • change to
<body onload="prettyPrint()" >

How to use:

Browsers display Tabs as 8 characters and not 4. So:
If you use Tabs in your source and Visual Studio 2010 then:
  1. ^A (Select All)
  2. Edit > Advanced > Untabify Selected Lines
  3. ^C (Copy)
  4. ^Z (Undo Untabify)
Clipboard now contains untabified source.
Edit the code sample you want to post
changeto
<&lt;
>&gt;
&&amp;
In the post enter:
<pre class="prettyprint lang-html">
Paste edited (as above) code here
</pre>
We can replace html in lang-html with:
"bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html", "java", "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh", "xhtml", "xml", "xsl".
The above languages are supported by default. You can add more if you include the corresponding js from src folder.

Numbering lines:

<pre class="prettyprint linenums:1">/* This is line 1 of my code
 * and here's line 2 */
print("I'm line number 3");
</pre>
gives (no, it doen't work- You have to go into prettify.css and delete everything about classes L0-L9:
li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8{list-style:none} li.L1,li.L3,li.L5,li.L7,li.L9{background:#eee})
/* This is line 1 of my code
 * and here's line 2 */
print("I'm line number 3");