SHJS Template Generation

Highlighted CSS looked sorta bad with the Eclipse theme. I tried a few other themes, but they didn't really change much. The reason for that is that all of those themes only cover a specific subset of the available shjs-classes and eventually a few more here and there. For the most part... not even half of the stuff is covered.

Writing your own theme, which covers at least the set of languages you're going to use is quite troublesome. You have to walk through all language scripts and assemble massive lists of classes with matching comments - before you can even start with the styling.

The solution? A quick'n'dirty hack of course! :)

import java.io.*;
import java.util.*;

public class SHJSTemplate{
    static HashMap map=new HashMap();
    public static void main(String[]args)throws Exception{
        new SHJSTemplate(args);
    }
    private void printUsageAndDie(){
        System.out.println(
            "usage:\n"+
            "java SHJSTemplate [-ds] [-o <outfile>] [-l <languages>] -d <lang directory>\n"+
            "  -ds   add default styles\n"+
            "  -o    file to write to\n"+
            "  -l    languages eg java+xml+php\n"+
            "  -d    path to the shjs/lang directory\n"+
            "  -?    or -help print this help screen\n\n"+
            "Eg:\n"+
            "java SHJSTemplate -ds -o sh_mytemplate.css -l css+html+jss -d x:/shjs/lang"
        );
        System.exit(0);
    }
    public SHJSTemplate(String[]args)throws Exception{
        boolean dstyles=false;
        String outName="sh_template.css";
        String dirName=null;
        String langPara="<all>";

        for(int i=0;i<args.length;i++){
            if(args[i].equals("-ds"))
                dstyles=true;
            else if(args[i].equals("-o"))
                outName=args[++i];
            else if(args[i].equals("-l"))
                langPara=args[++i];
            else if(args[i].equals("-d"))
                dirName=args[++i];
            else if(args[i].equals("-?")||args[i].equals("-help"))
                printUsageAndDie();
        }
        if(dirName==null)
            printUsageAndDie();

        System.out.println("default styles: "+dstyles);
        System.out.println("save as       : "+outName);
        System.out.println("languages     : "+langPara);
        System.out.println("dir           : "+dirName);

        String []lang=langPara.split("\\+");

        File dir=new File(dirName);
        File []files=dir.listFiles();
        System.out.println(files.length+" files found");
        for(int i=0;i<files.length;i++){
            if(!files[i].getName().endsWith(".min.js")){
                if(langPara.equals("<all>")){
                    read(files[i]);
                }else{
                    String s=files[i].getName();
                    s=s.substring(3,s.indexOf('.'));
                    System.out.println(s);
                    for(int k=0;k<lang.length;k++){
                        if(lang[k].equals(s)){
                            read(files[i]);
                            break;
                        }
                    }
                }
            }
        }
        Set entries=map.entrySet();
        Iterator iter=entries.iterator();
        BufferedWriter out=new BufferedWriter(new FileWriter(outName));
        while(iter.hasNext()){
            Map.Entry entry=(Map.Entry)iter.next();
            String style=(String)entry.getKey();
            Object []o=((HashSet)entry.getValue()).toArray();
            String []from=new String[o.length];
            for(int i=0;i<o.length;i++)
                from[i]=(String)o[i];
            Arrays.sort(from);
            out.write("/* ");
            for(int i=0;i<o.length;i++)
                out.write(from[i]+" ");
            out.write("*/\n");
            out.write("pre.sh_sourceCode span.");
            out.write(style);
            out.write("{\n");
            if(dstyles){
                out.write("  color: #000000;\n");
                out.write("  font-weight: normal;\n");
                out.write("  font-style: normal;\n");
            }
            out.write("}\n");
        }
        out.flush();
        out.close();
    }
    private void read(File f)throws Exception{
        System.out.println("reading:"+f.getName());
        String n=f.getName();
        String from=n.substring(n.indexOf('_')+1,n.indexOf('.'));
        BufferedReader in=new BufferedReader(new FileReader(f));
        String l="";
        while((l=in.readLine())!=null){
            String s=l.trim();
            if(s.startsWith("'style':")){
                //'style': 'sh_preproc'
                //'style': ['sh_normal', 'sh_port']
                String []p=s.substring(10,s.lastIndexOf('\'')).split("[^a-z_]");
                for(int i=0;i<p.length;i++){
                    if(p[i].trim().length()>0){
                        HashSet set;
                        if(map.containsKey(p[i])){
                            set=(HashSet)map.get(p[i]);
                        }else{
                            set=new HashSet();
                            map.put(p[i],set);
                        }
                        set.add(from);
                    }
                }
            }
        }
    }
}

With "-l java+xml+css+html+python+javascript+html" as parameter the result looks like this:

/* css html java javascript php python xml */
pre.sh_sourceCode span.sh_comment{
}
/* css java javascript php */
pre.sh_sourceCode span.sh_todo{
}
/* css html java javascript php python xml */
pre.sh_sourceCode span.sh_preproc{
}
/* css html java javascript php python xml */
pre.sh_sourceCode span.sh_keyword{
}
/* css java javascript php */
pre.sh_sourceCode span.sh_url{
}
/* java javascript */
pre.sh_sourceCode span.sh_specialchar{
}
/* css java javascript php */
pre.sh_sourceCode span.sh_cbracket{
}
/* css html java javascript php python xml */
pre.sh_sourceCode span.sh_symbol{
}
/* css */
pre.sh_sourceCode span.sh_property{
}
/* java javascript */
pre.sh_sourceCode span.sh_normal{
}
/* java javascript php python */
pre.sh_sourceCode span.sh_number{
}
/* css */
pre.sh_sourceCode span.sh_value{
}
/* java javascript php python */
pre.sh_sourceCode span.sh_function{
}
/* css */
pre.sh_sourceCode span.sh_selector{
}
/* css html java javascript php xml */
pre.sh_sourceCode span.sh_type{
}
/* css html java javascript php python xml */
pre.sh_sourceCode span.sh_string{
}
/* php python */
pre.sh_sourceCode span.sh_variable{
}

That's certainly a better starting point. :)

(I haven't written my own theme yet. At the time of writing the CSS still looks pretty dull.)

Source: SHJSTemplate.java
A complete 0.4.1 template (omitted -l switch): sh_fulltemplate.0.4.1.css (3kb)

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options