Revision: 1407
Author: sebastien.lelong
Date: Thu Oct 22 22:57:34 2009
Log: JAPP: publisher, config, glue-the-whole script
http://code.google.com/p/jallib/source/detail?r=1407

Added:
  /trunk/tools/japp/japp.sh
  /trunk/tools/japp/japp_config.py.tpl
  /trunk/tools/japp/publish.py
Modified:
  /trunk/tools/japp/dita2html.sh
  /trunk/tools/japp/htmlizer.py

=======================================
--- /dev/null
+++ /trunk/tools/japp/japp.sh   Thu Oct 22 22:57:34 2009
@@ -0,0 +1,93 @@
+#!/bin/bash
+
+# read on stdin strings formatting like
+#
+# <filename>.<extension>
+#
+# and process it.
+#  - if extension is "ditamap", produces a PDF file (this is a book)
+#  - if extension is not "ditamap", produces a HTML file
+#
+#  Processing goes like this:
+#  1. convert DITA file (dita2html.sh or ditamap2pdf.sh)
+#  2. prepare HTML for publishing (htmlize.py)
+#  3. format and send an email with content & attachements
+
+source japp_config.py
+if [ "$?" != "0" ]
+then
+   echo "Unable to source configuration file 'japp_config.py'"
+   exit 255
+fi
+
+
+if [ "$DITA_ROOTPATH" = "" ]
+then
+   echo "Please set DITA_ROOTPATH env. variable to the directory  
containing DITA files"
+   echo "(used by concatenating DITA_ROOTPATH and relative path given via  
stdin)"
+   exit 255
+fi
+
+if [ "$JAPP_ROOT" = "" ]
+then
+   echo "Please set JAPP_ROOT env. variable to the directory containing  
JAPP scripts"
+   exit 255
+fi
+
+if [ "$JAPP_EMAIL" = "" ]
+then
+   echo "Please set JAPP_EMAIL env. variable to an account monitored by  
the website (mailhandler)"
+   exit 255
+fi
+
+if [ "$JAPP_TMP" = "" ]
+then
+   JAPP_TMP=/tmp/japptmp
+fi
+
+while read DITAFILE
+do
+   echo "Processing file '$DITAFILE'..."
+   ext=`echo $DITAFILE | sed "s#.*\.\(.*\)\\$#\1#"`
+   noext=`echo $DITAFILE | sed "s#\(.*\)\..*\\$#\1#"`
+
+   if [ "$ext" = "ditamap" ]
+   then
+
+      echo "    Extension ditamap gives PDF, not implemented yet..."
+         continue
+
+   else
+
+         echo "    Extension $ext gives HTML, converting..."
+         echo
+         echo
+         sh $JAPP_ROOT/dita2html.sh $DITA_ROOTPATH/$DITAFILE $JAPP_TMP
+         if [ "$?" != "0" ]
+         then
+                echo
+                echo
+                echo "    Failed..."
+                continue
+         fi
+         echo "    Preparing HTML for Drupal publishing..."
+         python $JAPP_ROOT/htmlizer.py $JAPP_TMP/$noext.html
+         if [ "$?" != "0" ]
+         then
+                echo
+                echo
+                echo "    Failed..."
+                continue
+         fi
+         echo "    Sending content..."
+         pushd $JAPP_ROOT
+         python publish.py $JAPP_TMP/topublish
+         if [ "$?" != "0" ]
+         then
+                echo
+                echo
+                echo "    Failed..."
+         fi
+         popd
+   fi
+done
=======================================
--- /dev/null
+++ /trunk/tools/japp/japp_config.py.tpl        Thu Oct 22 22:57:34 2009
@@ -0,0 +1,49 @@
+################################
+# Main JAPP configuration file #
+################################
+
+# This is a python script, but this can also be
+# used as a shell script :)
+
+
+# DITA/Japp configuration
+#------------------------
+
+# Prefix path used to generated an absolute path
+# with submitted filenames. Ex: if you submit
+# "dirc/file.xml" and the absolute path is
+# "/the/path/dirc/file.xml", then DITA_ROOTPATH
+# is "/the/path"
+DITA_ROOTPATH=
+
+# Directory containing JAPP scripts (like this one)
+JAPP_ROOT=
+
+# Temporary directory used to produce output files
+JAPP_TMP=
+
+
+# Email configuration
+#--------------------
+
+# "From" address. Emails sent will use this address
+# and put it in "From" header. This address must match
+# someone with sufficient rights in Drupal website
+FROM_ADDRESS=""
+
+# Password associated to 'from_address'. This couple of
+# information allows to identify a user on Drupal's side.
+# This password will be put as a mailhandler command in emails.
+FROM_PASSWD=""
+
+# "To" address. Emails will be sent to this address.
+# This must corresponds to an address monitored by
+# mailhandler
+TO_ADDRESS=""
+
+# SMTP host/port used to send email
+SMTP_HOST="localhost"
+SMTP_PORT=25
+
+
+
=======================================
--- /dev/null
+++ /trunk/tools/japp/publish.py        Thu Oct 22 22:57:34 2009
@@ -0,0 +1,54 @@
+import smtplib, sys
+
+from email.mime.text import MIMEText
+from email.mime.image import MIMEImage
+from email.mime.multipart import MIMEMultipart
+
+import japp_config
+
+COMMASPACE = ', '
+
+try:
+    topublish = sys.argv[1]
+except IndexError:
+    print >> sys.stderr, "Please provide a readdy-to-publish directory"
+    sys.exit(255)
+
+title = file("%s/title" % topublish).read().strip()
+content = file("%s/content" % topublish).read()
+path = file("%s/path" % topublish).read()
+
+# Create the container (outer) email message.
+msg = MIMEMultipart()
+msg['Subject'] = title
+# me == the sender's email address
+me = japp_config.FROM_ADDRESS
+dest = japp_config.TO_ADDRESS
+msg['From'] = me
+msg['To'] = dest
+
+commands = """
+pass: %s
+path: content/%s
+
+""" % (japp_config.FROM_PASSWD,path)
+
+txt = MIMEText(commands + content)
+msg.attach(txt)
+
+# Assume we know that the image files are all in PNG format
+pngfiles =  
["/tmp/japptmp/topublish/i2c_connector.jpg","/tmp/japptmp/topublish/i2c_crimescene.jpg","/tmp/japptmp/topublish/i2c_details.jpg","/tmp/japptmp/topublish/i2c_pseudoecho.png","/tmp/japptmp/topublish/i2c_seb_mainboard_facade.jpg","/tmp/japptmp/topublish/i2c_seb_mainboard_up.jpg"]
+for file in pngfiles:
+    # Open the files in binary mode.  Let the MIMEImage class automatically
+    # guess the specific image type.
+    fp = open(file, 'rb')
+    img = MIMEImage(fp.read())
+    fp.close()
+    msg.attach(img)
+
+# Send the email via our own SMTP server.
+s = smtplib.SMTP()
+s.connect()
+s.sendmail(me, dest, msg.as_string())
+s.quit()
+
=======================================
--- /trunk/tools/japp/dita2html.sh      Wed Oct 21 23:03:57 2009
+++ /trunk/tools/japp/dita2html.sh      Thu Oct 22 22:57:34 2009
@@ -1,13 +1,13 @@
  #!/bin/bash

-DITAFILE=`pwd`/$1
+DITAFILE=$1
  OUTPUTDIR=$2
  TMPDIR=${OUTPUTDIR}.tmp

  if [ "$DITA_HOME" = "" ]
  then
      echo "DITA_HOME must be set to DITA installation directory"
-       exit 255
+    exit 255
  fi

  if [ "$DITAFILE" = "" ] || [ "$OUTPUTDIR" = "" ]
=======================================
--- /trunk/tools/japp/htmlizer.py       Wed Oct 21 23:03:57 2009
+++ /trunk/tools/japp/htmlizer.py       Thu Oct 22 22:57:34 2009
@@ -28,7 +28,9 @@
  # where to put all stuff to publish
  OUTPUT_DIR="topublish"
  # Filename for filtered HTML
-OUTPUT_FILE="content"
+CONTENT_FILE = "content"
+TITLE_FILE = "title"
+PATH_FILE = "path"

  try:
      hfile = sys.argv[1]
@@ -38,6 +40,7 @@

  # prepare ouput directory
  dirn = os.path.dirname(hfile)
+basen = os.path.basename(hfile)
  os.system("rm -f %s/%s/*" % (dirn,OUTPUT_DIR))
  os.system("mkdir -p %s/%s/" % (dirn,OUTPUT_DIR))

@@ -71,6 +74,7 @@

  # convert link URL
  pat = re.compile("\.html",re.I)
+noext = pat.sub("",basen)
  as_ = body.findAll("a")
  for a in as_:
      try:
@@ -99,11 +103,23 @@
  # produce twice title
  h1s = body.findAll("h1")
  if not h1s:
-    print >> sys.stderr, "No <h1> element found, assuming no title..."
+    print >> sys.stderr, "No <h1> element found, title will correspond to  
filename '%s'" % basen
+    title = basen
  else:
      # there should be only one element, but anyway remove first one
+    title = h1s[0].renderContents()
      h1s[0].replaceWith("")

  # we're done
-fout = file("%s/%s/%s" % (dirn,OUTPUT_DIR,OUTPUT_FILE),"w")
+fout = file("%s/%s/%s" % (dirn,OUTPUT_DIR,CONTENT_FILE),"w")
  fout.write(body.renderContents())
+fout.close()
+
+fout = file("%s/%s/%s" % (dirn,OUTPUT_DIR,TITLE_FILE),"w")
+fout.write(title)
+fout.close()
+
+fout = file("%s/%s/%s" % (dirn,OUTPUT_DIR,PATH_FILE),"w")
+fout.write(noext)
+fout.close()
+

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to