Posting Code

Hi all,

This is just a quick post to demonstrate how to add programming code to your posts. Rather than just pasting the code directly, there are a few shortcodes you can use to preserve the formatting & add syntax highlighting.

The most basic is: [code] your code here [/code] (without the underscores). Anything between these two tags will be treated like a piece of code, but no syntax highlighting will be applied. See below:

theta:/home/david # less /var/log/apache2/error_log
theta:/home/david # android-sdk/platform-tools/adb kill-server
theta:/home/david # android-sdk/platform-tools/adb start-server
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
theta:/home/david # less /var/log/apache2/error_log

There are also a few other languages you can use:
[ul]
[li]C
[/li][li]C++
[/li][li]Java
[/li][li]Visual Basic
[/li][li]PHP (custom)
[/li][li]XML/HTML
[/li][li]Javascript
[/li][li]Assembler
[/li][li]SQL
[/li][li]Batch
[/li][li]INI
[/li][/ul]
You can insert code fragments from these languages by using the appropriate tag - e.g. [java] your code here [/java] will highlight your code as a Java fragment. Just remember to remove the underscores from my sample tags!

Some examples of code with syntax highlighting are shown below:

[java]public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Hashtable<String, String> map = new Hashtable<String, String>();
map.put(“age”, “45”);
map.put(“zip”, “90210”);
map.put(“income”, “50000”);
//include additional metadata or settings in the hashmap
MMAdView adView = new MMAdView(this, MYAPID, MMAdView.BANNER_AD_BOTTOM, 30, map);
adView.setId(MMAdViewSDK.DEFAULT_VIEWID);
FrameLayout adFrameLayout = (FrameLayout)findViewById(R.id.adFrameLayout);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)
adFrameLayout.addView(adView, lp);
}
[/java]

<html><head><title></title>
	<script language="javascript" type="text/javascript" src="/javascript/common.js?plesk_version=psa-9.5.1-95100414.15"/></script>
	<script language="javascript" type="text/javascript" src="/javascript/prototype.js?plesk_version=psa-9.5.1-95100414.15"></script>
	<script>
		var opt_no_frames = false;
		var opt_integrated_mode = false;
	</script>
</head><body onLoad=";top.location='/login.php3';"></body></html>

If you’re going to be posting extremely long pieces of code, or multiple complete files, please consider adding your code as a zip attachment. That way people won’t have to scroll through a long post to read the actual content.

Cheers,

David

If you’re going to be posting extremely long pieces of code, or multiple complete files, please consider adding your code as a zip attachment. That way people won’t have to scroll through a long post to read the actual content.