Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,730,490 Members 40,476 Now Online
XDA Developers Android and Mobile Development Forum

[HOW-TO] Use Code Syntax Highlighting on XDA [Raise Awareness]

Tip us?
 
Androguide.fr
Old
(Last edited by Androguide.fr; 2nd February 2014 at 10:18 PM.)
#1  
Androguide.fr's Avatar
Recognized Contributor - OP
Thanks Meter 6909
Posts: 2,048
Join Date: Jul 2012
Location: Lyon, France

 
DONATE TO ME
Info 2 [HOW-TO] Use Code Syntax Highlighting on XDA [Raise Awareness]

XDA is an awesome place for developers to share their knowledge and help each other, no question about it.

However, in my opinion, one of the flaws that prevent the app development forums from being more popular is the [CODE] tags.
Indeed, these tags do not provide syntax highlighting, and when people start pasting their whole Java class, the lack of proper indentation plus the lack of syntax highlighting leaves us with a very off-putting chunk of code, and even if you intend to help in the first place, having to go through this unreadable code can discourage many.

I was ranting about this in the xda suggestions & feedback thread, when someone pointed-out the existence of the [PHP] tags, which provide syntax highlighting for php, and, the syntaxes of PHP and Java being somewhat similar, they also do the trick for Java.

Example:
PHP Code:
package com.androguide.recovery.emulator;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;

import android.util.Log;

public class 
CMDProcessor {

    private final 
String TAG getClass().getSimpleName();
    private static final 
boolean DEBUG false;
    private 
Boolean can_su;
    public 
SH sh;
    public 
SH su;

    public 
CMDProcessor() {
        
sh = new SH("sh");
        
su = new SH("su");
    }

    public 
SH suOrSH() {
        return 
canSU() ? su sh;
    }

    public 
boolean canSU() {
        return 
canSU(false);
    }

    public class 
CommandResult {
        private final 
String resultTag TAG '.' getClass().getSimpleName();
        public final 
String stdout;
        public final 
String stderr;
        public final 
Integer exit_value;

        
CommandResult(final Integer exit_value_in) {
            
this(exit_value_innullnull);
        }

        
CommandResult(final Integer exit_value_in, final String stdout_in,
                final 
String stderr_in) {
            
exit_value exit_value_in;
            
stdout stdout_in;
            
stderr stderr_in;
            if (
DEBUG)
                
Log.d(TAGresultTag "( exit_value=" exit_value
                        
", stdout=" stdout ", stderr=" stderr " )");
        }

        public 
boolean success() {
            return 
exit_value != null && exit_value == 0;
        }

    }

    public class 
SH {
        private 
String SHELL "sh";

        public 
SH(final String SHELL_in) {
            
SHELL SHELL_in;
        }

        private 
String getStreamLines(final InputStream is) {
            
String out null;
            
StringBuffer buffer null;
            final 
DataInputStream dis = new DataInputStream(is);
            try {
                if (
dis.available() > 0) {
                    
buffer = new StringBuffer(dis.readLine());
                    while (
dis.available() > 0) {
                        
buffer.append("\n").append(dis.readLine());
                    }
                }
                
dis.close();
            } catch (final 
Exception ex) {
                
Log.e(TAGex.getMessage());
            }
            if (
buffer != null) {
                
out buffer.toString();
            }
            return 
out;
        }

        public 
Process run(final String s) {
            
Process process null;
            try {
                
process Runtime.getRuntime().exec(SHELL);
                final 
DataOutputStream toProcess = new DataOutputStream(
                        
process.getOutputStream());
                
toProcess.writeBytes("exec " "\n");
                
toProcess.flush();
            } catch (final 
Exception e) {
                
Log.e(TAG,
                        
"Exception while trying to run: '" "' "
                                
e.getMessage());
                
process null;
            }
            return 
process;
        }

        public 
CommandResult runWaitFor(final String s) {
            if (
DEBUG)
                
Log.d(TAG"runWaitFor( " " )");
            final 
Process process run(s);
            
Integer exit_value null;
            
String stdout null;
            
String stderr null;
            if (
process != null) {
                try {
                    
exit_value process.waitFor();
                    
stdout getStreamLines(process.getInputStream());
                    
stderr getStreamLines(process.getErrorStream());
                } catch (final 
InterruptedException e) {
                    
Log.e(TAG"runWaitFor " e.toString());
                } catch (final 
NullPointerException e) {
                    
Log.e(TAG"runWaitFor " e.toString());
                }
            }
            return new 
CommandResult(exit_valuestdoutstderr);
        }
    }

    public 
boolean canSU(final boolean force_check) {
        if (
can_su == null || force_check) {
            final 
CommandResult r su.runWaitFor("id");
            final 
StringBuilder out = new StringBuilder();
            if (
r.stdout != null) {
                
out.append(r.stdout).append(" ; ");
            }
            if (
r.stderr != null) {
                
out.append(r.stderr);
            }
            
Log.d(TAG"canSU() su[" r.exit_value "]: " out);
            
can_su r.success();
        }
        return 
can_su;
    }


Oddly enough, the [PHP] tags also work for XML (while the [html] tags, that do exist, don't work):
PHP Code:
<LinearLayout
            android
:id="@+id/contentLayout"
            
android:layout_width="0dip"
            
android:layout_height="wrap_content"
            
android:layout_marginLeft="8dip"
            
android:layout_weight="90"
            
android:orientation="vertical" >

            <
TextView
                android
:id="@+id/title"
                
android:layout_width="wrap_content"
                
android:layout_height="wrap_content"
                
android:layout_marginLeft="8dip"
                
android:fontFamily="sans-serif-light"
                
android:textColor="#33B6EA"
                
android:textSize="22sp" />

            <
TextView
                android
:id="@+id/description"
                
android:layout_width="wrap_content"
                
android:layout_height="wrap_content"
                
android:layout_gravity="center_vertical"
                
android:layout_marginLeft="8dip"
                
android:layout_marginRight="8dip"
                
android:ellipsize="end"
                
android:fontFamily="sans-serif-light"
                
android:textColor="#232323"
                
android:maxLines="50"
                
android:textSize="16sp" />
 </
LinearLayout
This doesn't fix the indentation issues, but as you can see, the code is much more readable and welcoming.
By using the [PHP] tags instead of the [CODE] tags, you will be more likely to get answers to your questions involving code.

So Please, for the sake of the app development forums and the eyes of your readers:
USE THE [PHP] TAGS



Please raise awareness about this, suggest people who don't use the [PHP] tags to use them, and together we'll make this app development forum a better place than it already is

Quote:
My Contributions
(Apps, Librairies, Roms & Guides)
 

Pimp My Rom: Website / App / Thread

dSploit: Website / Github

Flash Gordon (Flash zips without a recovery): Thread

Universal init.d: App

PlayView/CardsUI Library: Portal / Github

HoloGraph Library: Portal / Github

Ubuntu Touch for i9300: Thread

Omni nightlies for Xperia Z1: Thread

Paranoid Android builds: Thread

Revive your bricked p68xx : Guide
Follow me on Github
The Following 3 Users Say Thank You to Androguide.fr For This Useful Post: [ Click to Expand ]
 
EatHeat
Old
#2  
EatHeat's Avatar
Recognized Developer
Thanks Meter 1389
Posts: 911
Join Date: Jan 2013
Location: Calcutta
Whoa! Never knew about this. Nice find.
[3.0+]Crius.
Customize your clock.


[2.2+]SnowBowl HD.
Enjoy the vacations!


 
SimplicityApks
Old
#3  
SimplicityApks's Avatar
Senior Member
Thanks Meter 307
Posts: 287
Join Date: May 2013
Location: Aachen
Wow, this is really cool! Thanks for finding. The only problem I have with the PHP tags is on mobile, tapatalk is obviously not supporting it at all:
Attached Thumbnails
Click image for larger version

Name:	uploadfromtaptalk1391374113843.jpg
Views:	34
Size:	54.6 KB
ID:	2555439  
The Following User Says Thank You to SimplicityApks For This Useful Post: [ Click to Expand ]
 
Androguide.fr
Old
#4  
Androguide.fr's Avatar
Recognized Contributor - OP
Thanks Meter 6909
Posts: 2,048
Join Date: Jul 2012
Location: Lyon, France

 
DONATE TO ME
Quote:
Originally Posted by SimplicityApks View Post
Wow, this is really cool! Thanks for finding. The only problem I have with the PHP tags is on mobile, tapatalk is obviously not supporting it at all:
Too bad, thanks for reporting
This obviously is just a workaround, xda could quite easily implement a dedicated syntax highlighting plugin for vBulletin as I suggested in the suggestions thread, but for the time being this is probably the closest thing we've got.

Quote:
My Contributions
(Apps, Librairies, Roms & Guides)
 

Pimp My Rom: Website / App / Thread

dSploit: Website / Github

Flash Gordon (Flash zips without a recovery): Thread

Universal init.d: App

PlayView/CardsUI Library: Portal / Github

HoloGraph Library: Portal / Github

Ubuntu Touch for i9300: Thread

Omni nightlies for Xperia Z1: Thread

Paranoid Android builds: Thread

Revive your bricked p68xx : Guide
Follow me on Github
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes