[latexila] Use ITS Tool for the templates



commit 331223c4ba4bd3778080a16008573e49ce3826dc
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Tue Jul 19 01:55:42 2011 +0200

    Use ITS Tool for the templates
    
    The templates are encoded in XML. Each template is composed of several
    chunks.
    
    For all the templates except the letter, there are three chunks, but
    only the second chunk is translatable (for the babel package or
    equivalent).
    
    For the letter, there is one big chunk with all the template. Decompose
    the letter into several strings to translate would not have been enough
    flexible, because in some languages we want to use for example another
    document class, with other commands, etc.
    
    There is a script: gen_tex.sh. This script removes the XML markups and
    generate .tex files.
    
    What is installed is only the .tex files. So there is one more step to
    do here (call the script).
    
    C/*.xml -> C/*.tex
    lang.po -> lang.mo -> lang-xml/*.xml -> lang/*.tex

 CMakeLists.txt                                 |    2 +
 TODO                                           |    4 -
 data/build_tools/CMakeLists.txt                |    2 -
 data/templates/C/article.xml                   |   28 +++++++++
 data/templates/C/beamer.xml                    |   20 ++++++
 data/templates/C/book.xml                      |   25 ++++++++
 data/templates/{letter-en.tex => C/letter.xml} |    8 ++-
 data/templates/C/report.xml                    |   28 +++++++++
 data/templates/CMakeLists.txt                  |   71 +++++++++++++++++++++-
 data/templates/article-de.tex                  |   20 ------
 data/templates/article-en.tex                  |   19 ------
 data/templates/article-es.tex                  |   20 ------
 data/templates/article-fr.tex                  |   20 ------
 data/templates/article-it.tex                  |   20 ------
 data/templates/article-pt.tex                  |   20 ------
 data/templates/beamer-de.tex                   |   12 ----
 data/templates/beamer-en.tex                   |   11 ---
 data/templates/beamer-es.tex                   |   12 ----
 data/templates/beamer-fr.tex                   |   12 ----
 data/templates/beamer-it.tex                   |   12 ----
 data/templates/beamer-pt.tex                   |   12 ----
 data/templates/book-de.tex                     |   17 -----
 data/templates/book-en.tex                     |   16 -----
 data/templates/book-es.tex                     |   17 -----
 data/templates/book-fr.tex                     |   17 -----
 data/templates/book-it.tex                     |   17 -----
 data/templates/book-pt.tex                     |   17 -----
 data/templates/fr.po                           |   78 ++++++++++++++++++++++++
 data/templates/gen_tex.sh                      |   13 ++++
 data/templates/letter-de.tex                   |   24 -------
 data/templates/letter-es.tex                   |   25 --------
 data/templates/letter-fr.tex                   |   25 --------
 data/templates/letter-it.tex                   |   25 --------
 data/templates/letter-pt.tex                   |   25 --------
 data/templates/report-de.tex                   |   20 ------
 data/templates/report-en.tex                   |   19 ------
 data/templates/report-es.tex                   |   20 ------
 data/templates/report-fr.tex                   |   20 ------
 data/templates/report-it.tex                   |   20 ------
 data/templates/report-pt.tex                   |   20 ------
 data/templates/templates.its                   |    8 +++
 data/templates/templates.pot                   |   53 ++++++++++++++++
 data/templates/update_templates_pot.sh         |    9 +++
 help/CMakeLists.txt                            |    2 -
 src/templates.vala                             |   44 +++++++++-----
 45 files changed, 368 insertions(+), 561 deletions(-)
---
diff --git a/CMakeLists.txt b/CMakeLists.txt
index dd00a9e..dca5048 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -71,6 +71,8 @@ link_libraries (
 	${X11_LIBRARIES}
 )
 
+include ("${latexila_SOURCE_DIR}/cmake/itstool.cmake")
+
 add_subdirectory (data)
 add_subdirectory (help)
 add_subdirectory (man)
diff --git a/TODO b/TODO
index 69ee81f..47653ff 100644
--- a/TODO
+++ b/TODO
@@ -6,10 +6,6 @@ See also the Roadmap:
 LaTeXila 2.2
 ============
 
-- use ITS tool (http://itstool.org/):
-	- for the templates
-	- ...?
-
 - Build Tools:
 	- Some problems with the latex and latexmk post processors
 
diff --git a/data/build_tools/CMakeLists.txt b/data/build_tools/CMakeLists.txt
index 61942d3..756c1ef 100644
--- a/data/build_tools/CMakeLists.txt
+++ b/data/build_tools/CMakeLists.txt
@@ -1,5 +1,3 @@
-include ("${latexila_SOURCE_DIR}/cmake/itstool.cmake")
-
 itstool (
 	"build_tools" # target name
 	"${latexila_SOURCE_DIR}/data/build_tools" # src dir
diff --git a/data/templates/C/article.xml b/data/templates/C/article.xml
new file mode 100644
index 0000000..fc11ea1
--- /dev/null
+++ b/data/templates/C/article.xml
@@ -0,0 +1,28 @@
+<template xmlns:its="http://www.w3.org/2005/11/its";>
+<chunk its:translate="no">
+\documentclass[a4paper,11pt]{article}
+\usepackage[T1]{fontenc}
+\usepackage[utf8]{inputenc}
+\usepackage{lmodern}
+</chunk>
+<chunk>
+% babel package or equivalent
+</chunk>
+<chunk its:translate="no">
+
+\title{}
+\author{}
+
+\begin{document}
+
+\maketitle
+\tableofcontents
+
+\begin{abstract}
+\end{abstract}
+
+\section{}
+
+\end{document}
+</chunk>
+</template>
diff --git a/data/templates/C/beamer.xml b/data/templates/C/beamer.xml
new file mode 100644
index 0000000..bdf4829
--- /dev/null
+++ b/data/templates/C/beamer.xml
@@ -0,0 +1,20 @@
+<template xmlns:its="http://www.w3.org/2005/11/its";>
+<chunk its:translate="no">
+\documentclass{beamer}
+\usepackage[T1]{fontenc}
+\usepackage[utf8]{inputenc}
+\usepackage{lmodern}
+</chunk>
+<chunk>
+% babel package or equivalent
+</chunk>
+<chunk its:translate="no">
+
+\title{}
+\author{}
+
+\begin{document}
+
+\end{document}
+</chunk>
+</template>
diff --git a/data/templates/C/book.xml b/data/templates/C/book.xml
new file mode 100644
index 0000000..1b5a898
--- /dev/null
+++ b/data/templates/C/book.xml
@@ -0,0 +1,25 @@
+<template xmlns:its="http://www.w3.org/2005/11/its";>
+<chunk its:translate="no">
+\documentclass[a4paper,11pt]{book}
+\usepackage[T1]{fontenc}
+\usepackage[utf8]{inputenc}
+\usepackage{lmodern}
+</chunk>
+<chunk>
+% babel package or equivalent
+</chunk>
+<chunk its:translate="no">
+
+\title{}
+\author{}
+
+\begin{document}
+
+\maketitle
+\tableofcontents
+
+\chapter{}
+
+\end{document}
+</chunk>
+</template>
diff --git a/data/templates/letter-en.tex b/data/templates/C/letter.xml
similarity index 82%
rename from data/templates/letter-en.tex
rename to data/templates/C/letter.xml
index f68d50b..f6adc66 100644
--- a/data/templates/letter-en.tex
+++ b/data/templates/C/letter.xml
@@ -1,3 +1,5 @@
+<template xmlns:its="http://www.w3.org/2005/11/its";>
+<chunk>
 \documentclass[a4paper,11pt]{letter}
 \usepackage[T1]{fontenc}
 \usepackage[utf8]{inputenc}
@@ -9,7 +11,7 @@
 \begin{document}
 
 \begin{letter}{Destination\\Address of the destination\\Phone number of the destination}
-	
+
 \opening{Dear Sir,}
 
 % corps of the letter
@@ -21,4 +23,6 @@
 %\encl{Enclosures}
 
 \end{letter}
-\end{document}
\ No newline at end of file
+\end{document}
+</chunk>
+</template>
diff --git a/data/templates/C/report.xml b/data/templates/C/report.xml
new file mode 100644
index 0000000..7e2d4f9
--- /dev/null
+++ b/data/templates/C/report.xml
@@ -0,0 +1,28 @@
+<template xmlns:its="http://www.w3.org/2005/11/its";>
+<chunk its:translate="no">
+\documentclass[a4paper,11pt]{report}
+\usepackage[T1]{fontenc}
+\usepackage[utf8]{inputenc}
+\usepackage{lmodern}
+</chunk>
+<chunk>
+% babel package or equivalent
+</chunk>
+<chunk its:translate="no">
+
+\title{}
+\author{}
+
+\begin{document}
+
+\maketitle
+\tableofcontents
+
+\begin{abstract}
+\end{abstract}
+
+\chapter{}
+
+\end{document}
+</chunk>
+</template>
diff --git a/data/templates/CMakeLists.txt b/data/templates/CMakeLists.txt
index 185f324..ead87fd 100644
--- a/data/templates/CMakeLists.txt
+++ b/data/templates/CMakeLists.txt
@@ -1,2 +1,69 @@
-file (GLOB templates *.tex)
-install (FILES ${templates} DESTINATION ${DATA_DIR}/templates)
+set (src_dir "${latexila_SOURCE_DIR}/data/templates")
+set (tmp_dir "${latexila_BINARY_DIR}/data/templates")
+set (install_dir "${DATA_DIR}/templates")
+
+# Get list of XML files
+file (GLOB path_files "${src_dir}/C/*")
+set (list_files)
+foreach (path_file ${path_files})
+	get_filename_component (file ${path_file} NAME_WE)
+	set (list_files ${list_files} "${file}.tex")
+endforeach ()
+
+###
+# Generate default *.tex
+###
+
+# Get the paths of the new files
+set (lang "C")
+set (lang_files)
+foreach (file ${list_files})
+	set (lang_files ${lang_files} "${tmp_dir}/${lang}/${file}")
+endforeach ()
+
+# Generate the *.tex files from the *.xml
+add_custom_command (
+	OUTPUT ${lang_files}
+	COMMAND mkdir ${tmp_dir}/${lang}
+	COMMAND ${src_dir}/gen_tex.sh ${src_dir}/${lang} ${tmp_dir}/${lang}
+	DEPENDS ${path_files}
+)
+
+# Install the directory (which contains only the *.tex files)
+install (DIRECTORY ${tmp_dir}/${lang} DESTINATION ${install_dir})
+
+set (all_new_files ${lang_files})
+
+###
+# Generate *.tex foreach language
+###
+
+file (GLOB po_files "${src_dir}/*.po")
+foreach (po_file ${po_files})
+	# Get the language name
+	get_filename_component (lang ${po_file} NAME_WE)
+
+	# Get the paths of the new files
+	set (lang_files)
+	foreach (file ${list_files})
+		set (lang_files ${lang_files} "${tmp_dir}/${lang}/${file}")
+	endforeach ()
+
+	# Generate the new files from the .po
+	set (mo_file "${tmp_dir}/${lang}.mo")
+	add_custom_command (
+		OUTPUT ${lang_files}
+		COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${mo_file} ${po_file}
+		COMMAND mkdir ${tmp_dir}/${lang}-xml ${tmp_dir}/${lang}
+		COMMAND itstool -i ${src_dir}/templates.its -m ${mo_file} -o ${tmp_dir}/${lang}-xml/ ${path_files}
+		COMMAND ${src_dir}/gen_tex.sh ${tmp_dir}/${lang}-xml ${tmp_dir}/${lang}
+		DEPENDS ${po_file}
+	)
+
+	# Install the directory (which contains only the *.tex files)
+	install (DIRECTORY ${tmp_dir}/${lang} DESTINATION ${install_dir})
+
+	set (all_new_files ${all_new_files} ${lang_files})
+endforeach ()
+
+add_custom_target ("templates" ALL DEPENDS ${all_new_files})
diff --git a/data/templates/fr.po b/data/templates/fr.po
new file mode 100644
index 0000000..f1db134
--- /dev/null
+++ b/data/templates/fr.po
@@ -0,0 +1,78 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: LaTeXila 2.1\n"
+"POT-Creation-Date: 2011-07-18 23:03+0200\n"
+"PO-Revision-Date: 2011-07-18 17:31+0100\n"
+"Last-Translator: SÃbastien Wilmet <sebastien wilmet gmail com>\n"
+"Language-Team: \n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"X-Poedit-Language: French\n"
+
+#: C/article.xml:8(template/chunk) C/beamer.xml:8(template/chunk)
+#: C/book.xml:8(template/chunk) C/report.xml:8(template/chunk)
+#, no-wrap
+msgid ""
+"\n"
+"% babel package or equivalent\n"
+msgstr ""
+"\n"
+"\\usepackage[francais]{babel}\n"
+
+#: C/letter.xml:2(template/chunk)
+#, no-wrap
+msgid ""
+"\n"
+"\\documentclass[a4paper,11pt]{letter}\n"
+"\\usepackage[T1]{fontenc}\n"
+"\\usepackage[utf8]{inputenc}\n"
+"\\usepackage{lmodern}\n"
+"\n"
+"\\address{Your name\\\\Your address\\\\Your phone number}\n"
+"\\signature{Your name}\n"
+"\n"
+"\\begin{document}\n"
+"\n"
+"\\begin{letter}{Destination\\\\Address of the destination\\\\Phone number of the destination}\n"
+"\n"
+"\\opening{Dear Sir,}\n"
+"\n"
+"% corps of the letter\n"
+"\n"
+"\\closing{Yours sincerely,}\n"
+"\n"
+"%\\cc{Other destination}\n"
+"%\\ps{PS: PostScriptum}\n"
+"%\\encl{Enclosures}\n"
+"\n"
+"\\end{letter}\n"
+"\\end{document}\n"
+msgstr ""
+"\n"
+"\\documentclass[a4paper,11pt]{letter}\n"
+"\\usepackage[T1]{fontenc}\n"
+"\\usepackage[utf8]{inputenc}\n"
+"\\usepackage{lmodern}\n"
+"\\usepackage[francais]{babel}\n"
+"\n"
+"\\address{Votre nom\\\\Votre adresse\\\\Votre numÃro de tÃlÃphone}\n"
+"\\signature{Votre nom}\n"
+"\n"
+"\\begin{document}\n"
+"\n"
+"\\begin{letter}{Destinataire\\\\Son adresse\\\\Son numÃro de tÃlÃphone}\n"
+"\n"
+"\\opening{Cher destinataire,}\n"
+"\n"
+"% corps de la lettre\n"
+"\n"
+"\\closing{Salutations,}\n"
+"\n"
+"%\\cc{Autres destinataires}\n"
+"%\\ps{PS: PostScriptum}\n"
+"%\\encl{Liste d'objets inclus dans l'enveloppe}\n"
+"\n"
+"\\end{letter}\n"
+"\\end{document}\n"
diff --git a/data/templates/gen_tex.sh b/data/templates/gen_tex.sh
new file mode 100755
index 0000000..6a044e3
--- /dev/null
+++ b/data/templates/gen_tex.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+# remove the trailing '/' from the directories
+input_dir="${1%/}"
+output_dir="${2%/}"
+
+for xml_path in $input_dir/*.xml; do
+	xml_filename=`basename $xml_path`
+	tex_filename="${xml_filename%.xml}.tex"
+	tex_path="$output_dir/$tex_filename"
+
+	sed -e '/^</d' -e '/^% babel package or equivalent/d' < $xml_path > $tex_path
+done
diff --git a/data/templates/templates.its b/data/templates/templates.its
new file mode 100644
index 0000000..60807c4
--- /dev/null
+++ b/data/templates/templates.its
@@ -0,0 +1,8 @@
+<its:rules
+    xmlns:its="http://www.w3.org/2005/11/its";
+    xmlns:itst="http://itstool.org/extensions/";
+    its:version="1.0">
+
+    <itst:preserveSpaceRule preserveSpace="yes" selector="//chunk" />
+
+</its:rules>
diff --git a/data/templates/templates.pot b/data/templates/templates.pot
new file mode 100644
index 0000000..d9a1542
--- /dev/null
+++ b/data/templates/templates.pot
@@ -0,0 +1,53 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"POT-Creation-Date: 2011-07-19 00:49+0200\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL ADDRESS>\n"
+"Language-Team: LANGUAGE <LL li org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: C/article.xml:8(template/chunk)
+#: C/beamer.xml:8(template/chunk)
+#: C/book.xml:8(template/chunk)
+#: C/report.xml:8(template/chunk)
+#, no-wrap
+msgid ""
+"\n"
+"% babel package or equivalent\n"
+""
+msgstr ""
+
+#: C/letter.xml:2(template/chunk)
+#, no-wrap
+msgid ""
+"\n"
+"\\documentclass[a4paper,11pt]{letter}\n"
+"\\usepackage[T1]{fontenc}\n"
+"\\usepackage[utf8]{inputenc}\n"
+"\\usepackage{lmodern}\n"
+"\n"
+"\\address{Your name\\\\Your address\\\\Your phone number}\n"
+"\\signature{Your name}\n"
+"\n"
+"\\begin{document}\n"
+"\n"
+"\\begin{letter}{Destination\\\\Address of the destination\\\\Phone number of the destination}\n"
+"\n"
+"\\opening{Dear Sir,}\n"
+"\n"
+"% corps of the letter\n"
+"\n"
+"\\closing{Yours sincerely,}\n"
+"\n"
+"%\\cc{Other destination}\n"
+"%\\ps{PS: PostScriptum}\n"
+"%\\encl{Enclosures}\n"
+"\n"
+"\\end{letter}\n"
+"\\end{document}\n"
+""
+msgstr ""
+
diff --git a/data/templates/update_templates_pot.sh b/data/templates/update_templates_pot.sh
new file mode 100755
index 0000000..44d8584
--- /dev/null
+++ b/data/templates/update_templates_pot.sh
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+pot_file="templates.pot"
+
+itstool -o $pot_file -i templates.its C/*.xml
+
+for po_file in *.po; do
+	msgmerge --update --quiet --backup=none $po_file $pot_file
+done
diff --git a/help/CMakeLists.txt b/help/CMakeLists.txt
index ff3c462..bbe9943 100644
--- a/help/CMakeLists.txt
+++ b/help/CMakeLists.txt
@@ -1,5 +1,3 @@
-include ("${latexila_SOURCE_DIR}/cmake/itstool.cmake")
-
 itstool (
 	"documentation" # target name
 	"${latexila_SOURCE_DIR}/help" # src dir
diff --git a/src/templates.vala b/src/templates.vala
index d7bd5e9..82ca740 100644
--- a/src/templates.vala
+++ b/src/templates.vala
@@ -46,20 +46,11 @@ public class Templates : GLib.Object
 
         add_template_from_string (default_store, _("Empty"), "empty", "");
 
-        add_template_from_file (default_store, _("Article"), "article",
-            File.new_for_path (Config.DATA_DIR + "/templates/" + _("article-en.tex")));
-
-        add_template_from_file (default_store, _("Report"), "report",
-            File.new_for_path (Config.DATA_DIR + "/templates/" + _("report-en.tex")));
-
-        add_template_from_file (default_store, _("Book"), "book",
-            File.new_for_path (Config.DATA_DIR + "/templates/" + _("book-en.tex")));
-
-        add_template_from_file (default_store, _("Letter"), "letter",
-            File.new_for_path (Config.DATA_DIR + "/templates/" + _("letter-en.tex")));
-
-        add_template_from_file (default_store, _("Presentation"), "beamer",
-            File.new_for_path (Config.DATA_DIR + "/templates/" + _("beamer-en.tex")));
+        add_default_template (_("Article"),         "article",  "article.tex");
+        add_default_template (_("Report"),          "report",   "report.tex");
+        add_default_template (_("Book"),            "book",     "book.tex");
+        add_default_template (_("Letter"),          "letter",   "letter.tex");
+        add_default_template (_("Presentation"),    "beamer",   "beamer.tex");
 
         /* personal templates */
         personal_store = new ListStore (TemplateColumn.N_COLUMNS, typeof (Gdk.Pixbuf),
@@ -311,7 +302,7 @@ public class Templates : GLib.Object
         Dialog dialog = new Dialog.with_buttons (_("Delete Template(s)..."), parent,
             DialogFlags.NO_SEPARATOR,
             Stock.DELETE, ResponseType.ACCEPT,
-            Stock.OK, ResponseType.REJECT,
+            Stock.CLOSE, ResponseType.REJECT,
             null);
 
         dialog.set_default_size (400, 200);
@@ -394,6 +385,29 @@ public class Templates : GLib.Object
         }
     }
 
+    private void add_default_template (string name, string icon_id, string filename)
+    {
+        File[] files = {};
+
+        unowned string[] language_names = Intl.get_language_names ();
+        foreach (string language_name in language_names)
+        {
+            files += File.new_for_path (Path.build_filename (Config.DATA_DIR,
+                "templates", language_name, filename));
+        }
+
+        foreach (File file in files)
+        {
+            if (! file.query_exists ())
+                continue;
+
+            add_template_from_file (default_store, name, icon_id, file);
+            return;
+        }
+
+        stderr.printf ("Warning: template \"%s\" not found.\n", name);
+    }
+
     private IconView create_icon_view (ListStore store)
     {
         IconView icon_view = new IconView.with_model (store);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]