Tag Archives: xbnjava

An example of how I use XBN-Java and Template Feather: Filling live code samples into blog posts

I’m writing a many-part Python Django tutorial, which is a step-by-step for implementing authentication (create/delete web account, login/logout, change/reset password). Each post has multiple code samples, each of which are live code. I therefore have to manually duplicate the code into the post on each change I make.

Instead, I’ve written the following for each post, which manually reads in the live code files, and inserts them into the proper spots in the post-template. (This is only for code-samples that are of the entire file, not a snippet.)

This is built with XBN-Java 0.1.5.1 and Template Featherweight 0.1.1.3

  import  com.github.aliteralmind.templatefeather.FeatherTemplate;
   import  com.github.aliteralmind.templatefeather.GapCharConfig;
   import  com.github.aliteralmind.templatefeather.Resettable;
   import  com.github.xbn.io.Overwrite;
   import  com.github.xbn.io.PlainTextFileUtil;
/**
 * cd /home/jeffy/django_files/django_auth_lifecycle/non_django_files/wordpress_posts/build/
 * java -classpath .:/home/jeffy/django_files/django_auth_lifecycle/non_django_files/wordpress_posts/build/dependency_jars/xbnjava-0.1.5.1.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rt.jar:/home/jeffy/django_files/django_auth_lifecycle/non_django_files/wordpress_posts/build/dependency_jars/commons-io-2.4.jar:/home/jeffy/django_files/django_auth_lifecycle/non_django_files/wordpress_posts/build/dependency_jars/templatefeather-0.1.1.3.jar BuildPartThree
 */
public class BuildPartThree  {
   public static final void main(String[] ignored)  {
      //Configuration
         String inputRoot = "/home/jeffy/django_files/django_auth_lifecycle/";
         String postRoot = inputRoot + "non_django_files/wordpress_posts/";
         String inputPath = postRoot + "03_main_view.html.html";
         String outputPath = postRoot + "build/output/03_main_view.html__built.html";

         GapCharConfig gapcfg = new GapCharConfig('@', '@',
            "__AT_SIGN_START__", "__AT_SIGN_END__");


      //Read in raw template
         String origText = PlainTextFileUtil.getText(inputPath, "origText");
         FeatherTemplate tmpl = new FeatherTemplate(origText, gapcfg,
            Resettable.NO, System.out);

      //Read in source code, one per gap
         String alRoot = inputRoot + "djauth_root/auth_lifecycle/";

         String viewMainDotPy = PlainTextFileUtil.getText(alRoot + "view__main.py", "viewMainDotPy").
            trim();
         String mainPageDotHtml = PlainTextFileUtil.getText(alRoot + "main_page.html", "mainPageDotHtml").
            trim();
         String testViewMainDotPy = PlainTextFileUtil.getText(alRoot + "test__view_main.py", "testViewMainDotPy").
            trim();
         String alUrlsDotPy = PlainTextFileUtil.getText(alRoot + "urls.py", "alUrlsDotPy").
            trim();

      //Fill in gaps with read-in code

         //Example gap: @view__main_dot_py@

         String rendered = tmpl.
               fill("view__main_dot_py", viewMainDotPy).
               fill("main_page_dot_py", mainPageDotHtml).
               fill("test__view_main_dot_py", testViewMainDotPy).
               fill("al_urls_dot_py", alUrlsDotPy).
            getFilled();

      //Global replacement
         rendered = rendered.replace("/home/jeffy/", "/home/myname/");

      //Write out result
         PlainTextFileUtil.openWriteClose(outputPath, "outputPath",
            Overwrite.YES, rendered);
   }
}