[latexila/wip/latexila-next] LatexilaPostProcessorLatexmk (not finished)



commit f354b4624d0272da9a28a2d9d18f024130ee571f
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Sep 17 22:15:11 2014 +0200

    LatexilaPostProcessorLatexmk (not finished)

 docs/reference/latexila-docs.xml                  |    1 +
 docs/reference/latexila-sections.txt              |   17 +
 src/liblatexila/Makefile.am                       |    2 +
 src/liblatexila/latexila-post-processor-latexmk.c |  382 +++++++++++++++++++++
 src/liblatexila/latexila-post-processor-latexmk.h |   57 +++
 src/liblatexila/latexila-types.h                  |    1 +
 6 files changed, 460 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/latexila-docs.xml b/docs/reference/latexila-docs.xml
index 0a693c3..f8e08c3 100644
--- a/docs/reference/latexila-docs.xml
+++ b/docs/reference/latexila-docs.xml
@@ -20,6 +20,7 @@
     <xi:include href="xml/post-processor.xml"/>
     <xi:include href="xml/post-processor-all-output.xml"/>
     <xi:include href="xml/post-processor-latex.xml"/>
+    <xi:include href="xml/post-processor-latexmk.xml"/>
     <xi:include href="xml/synctex.xml"/>
     <xi:include href="xml/utils.xml"/>
   </chapter>
diff --git a/docs/reference/latexila-sections.txt b/docs/reference/latexila-sections.txt
index a589d6c..6f623f5 100644
--- a/docs/reference/latexila-sections.txt
+++ b/docs/reference/latexila-sections.txt
@@ -191,6 +191,23 @@ latexila_post_processor_latex_get_type
 </SECTION>
 
 <SECTION>
+<FILE>post-processor-latexmk</FILE>
+<TITLE>LatexilaPostProcessorLatexmk</TITLE>
+LatexilaPostProcessorLatexmk
+latexila_post_processor_latexmk_new
+<SUBSECTION Standard>
+LATEXILA_IS_POST_PROCESSOR_LATEXMK
+LATEXILA_IS_POST_PROCESSOR_LATEXMK_CLASS
+LATEXILA_POST_PROCESSOR_LATEXMK
+LATEXILA_POST_PROCESSOR_LATEXMK_CLASS
+LATEXILA_POST_PROCESSOR_LATEXMK_GET_CLASS
+LATEXILA_TYPE_POST_PROCESSOR_LATEXMK
+LatexilaPostProcessorLatexmkClass
+LatexilaPostProcessorLatexmkPrivate
+latexila_post_processor_latexmk_get_type
+</SECTION>
+
+<SECTION>
 <FILE>synctex</FILE>
 <TITLE>LatexilaSynctex</TITLE>
 LatexilaSynctex
diff --git a/src/liblatexila/Makefile.am b/src/liblatexila/Makefile.am
index e2e33fa..c0998e1 100644
--- a/src/liblatexila/Makefile.am
+++ b/src/liblatexila/Makefile.am
@@ -21,6 +21,7 @@ liblatexila_headers =                         \
        latexila-post-processor.h               \
        latexila-post-processor-all-output.h    \
        latexila-post-processor-latex.h         \
+       latexila-post-processor-latexmk.h       \
        latexila-synctex.h                      \
        latexila-types.h                        \
        latexila-utils.h
@@ -35,6 +36,7 @@ liblatexila_sources =                         \
        latexila-post-processor.c               \
        latexila-post-processor-all-output.c    \
        latexila-post-processor-latex.c         \
+       latexila-post-processor-latexmk.c       \
        latexila-synctex.c                      \
        latexila-utils.c
 
diff --git a/src/liblatexila/latexila-post-processor-latexmk.c 
b/src/liblatexila/latexila-post-processor-latexmk.c
new file mode 100644
index 0000000..b7a255f
--- /dev/null
+++ b/src/liblatexila/latexila-post-processor-latexmk.c
@@ -0,0 +1,382 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * SECTION:post-processor-latexmk
+ * @title: LatexilaPostProcessorLatexmk
+ * @short_description: latexmk post-processor
+ *
+ * A post-processor used for the latexmk command. Internally, this
+ * post-processor uses other ones: #LatexilaPostProcessorLatex and
+ * #LatexilaPostProcessorAllOutput.
+ */
+
+#include "latexila-post-processor-latexmk.h"
+#include "latexila-build-view.h"
+
+typedef enum
+{
+  STATE_FETCH_SUB_TITLE_START,
+  STATE_FETCH_SUB_TITLE_END,
+  STATE_FETCH_SUB_COMMAND_START,
+  STATE_FETCH_SUB_COMMAND_END
+} State;
+
+struct _LatexilaPostProcessorLatexmkPrivate
+{
+  /* The tree of all messages. */
+  GQueue *messages;
+
+  State state;
+
+  /* Number of separators encountered for the current state. */
+  gint separator_count;
+
+  LatexilaBuildMsg *last_latex_sub_command;
+  GQueue *last_latex_lines;
+
+  guint last_rule_is_latex_rule : 1;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (LatexilaPostProcessorLatexmk,
+                            latexila_post_processor_latexmk,
+                            LATEXILA_TYPE_POST_PROCESSOR)
+
+static void
+latexila_post_processor_latexmk_start (LatexilaPostProcessor *post_processor,
+                                       GFile                 *file)
+{
+}
+
+static void
+latexila_post_processor_latexmk_end (LatexilaPostProcessor *post_processor)
+{
+}
+
+static void
+add_top_level_message (LatexilaPostProcessorLatexmk *pp,
+                       LatexilaBuildMsg             *msg)
+{
+  g_assert (msg != NULL);
+
+  msg->type = LATEXILA_BUILD_MSG_TYPE_JOB_SUB_COMMAND;
+
+  /* Do not expand the row, so the user has first a global view of what have
+   * been executed.
+   */
+  msg->expand = FALSE;
+
+  if (msg->children == NULL)
+    msg->children = g_queue_new ();
+
+  g_queue_push_tail (pp->priv->messages, msg);
+}
+
+static void
+add_sub_message (LatexilaPostProcessorLatexmk *pp,
+                 LatexilaBuildMsg             *sub_msg)
+{
+  LatexilaBuildMsg *last_toplevel_msg;
+
+  g_assert (sub_msg != NULL);
+
+  last_toplevel_msg = g_queue_peek_tail (pp->priv->messages);
+
+  if (last_toplevel_msg == NULL)
+    {
+      g_warning ("PostProcessorLatexmk: try to add a sub-message without a top-level message.");
+      latexila_build_msg_free (sub_msg);
+      return;
+    }
+
+  sub_msg->type = LATEXILA_BUILD_MSG_TYPE_INFO;
+
+  g_assert (last_toplevel_msg->children != NULL);
+  g_queue_push_tail (last_toplevel_msg->children, sub_msg);
+}
+
+static void
+set_last_latex_sub_command (LatexilaPostProcessorLatexmk *pp,
+                            LatexilaBuildMsg             *msg)
+{
+  if (pp->priv->last_latex_lines != NULL)
+    g_queue_free_full (pp->priv->last_latex_lines, g_free);
+
+  pp->priv->last_latex_lines = g_queue_new ();
+  pp->priv->last_latex_sub_command = msg;
+}
+
+static gboolean
+is_separator (const gchar *line)
+{
+  return g_str_has_prefix (line, "------------");
+}
+
+static void
+fetch_sub_title (LatexilaPostProcessorLatexmk *pp,
+                 gchar                        *line)
+{
+  static GRegex *regex_sub_title = NULL;
+  GMatchInfo *match_info;
+
+  g_assert (pp->priv->state == STATE_FETCH_SUB_TITLE_START ||
+            pp->priv->state == STATE_FETCH_SUB_TITLE_END);
+
+  if (G_UNLIKELY (regex_sub_title == NULL))
+    {
+      GError *error = NULL;
+
+      regex_sub_title = g_regex_new ("Run number \\d+ of rule '(?P<rule>.*)'",
+                                     G_REGEX_OPTIMIZE,
+                                     0,
+                                     &error);
+
+      if (error != NULL)
+        {
+          g_warning ("PostProcessorLatexmk: %s", error->message);
+          g_error_free (error);
+          return;
+        }
+    }
+
+  if (pp->priv->state == STATE_FETCH_SUB_TITLE_END)
+    {
+      g_assert (pp->priv->separator_count == 1);
+
+      if (is_separator (line))
+        {
+          pp->priv->separator_count = 0;
+          pp->priv->state = STATE_FETCH_SUB_COMMAND_START;
+        }
+
+      goto end;
+    }
+
+  if (is_separator (line))
+    {
+      pp->priv->separator_count++;
+
+      if (pp->priv->separator_count == 2)
+        {
+          pp->priv->separator_count = 0;
+          g_warning ("PostProcessorLatexmk: fetch sub-title failed, try again.");
+        }
+
+      goto end;
+    }
+
+  if (pp->priv->separator_count != 1)
+    goto end;
+
+  g_regex_match (regex_sub_title, line, 0, &match_info);
+
+  if (g_match_info_matches (match_info))
+    {
+      LatexilaBuildMsg *msg;
+      gchar *rule;
+
+      msg = latexila_build_msg_new ();
+      msg->text = line;
+      line = NULL;
+
+      add_top_level_message (pp, msg);
+      pp->priv->state = STATE_FETCH_SUB_TITLE_END;
+
+      rule = g_match_info_fetch_named (match_info, "rule");
+
+      pp->priv->last_rule_is_latex_rule = (g_strcmp0 (rule, "latex") == 0 ||
+                                           g_strcmp0 (rule, "pdflatex") == 0);
+
+      if (pp->priv->last_rule_is_latex_rule)
+        set_last_latex_sub_command (pp, msg);
+
+      g_free (rule);
+    }
+
+  g_match_info_free (match_info);
+
+end:
+  g_free (line);
+}
+
+static void
+fetch_sub_command (LatexilaPostProcessorLatexmk *pp,
+                   gchar                        *line)
+{
+  static GRegex *regex_sub_command = NULL;
+  GMatchInfo *match_info;
+
+  g_assert (pp->priv->state == STATE_FETCH_SUB_COMMAND_START ||
+            pp->priv->state == STATE_FETCH_SUB_COMMAND_END);
+
+  if (G_UNLIKELY (regex_sub_command == NULL))
+    {
+      GError *error = NULL;
+
+      regex_sub_command = g_regex_new ("Running '(?P<command>.*)'",
+                                       G_REGEX_OPTIMIZE,
+                                       0,
+                                       &error);
+
+      if (error != NULL)
+        {
+          g_warning ("PostProcessorLatexmk: %s", error->message);
+          g_error_free (error);
+          return;
+        }
+    }
+
+  if (pp->priv->state == STATE_FETCH_SUB_COMMAND_END)
+    {
+      g_assert (pp->priv->separator_count == 1);
+
+      if (is_separator (line))
+        {
+          pp->priv->separator_count = 0;
+          /* TODO set next state */
+          pp->priv->state = STATE_FETCH_SUB_TITLE_START;
+        }
+
+      goto end;
+    }
+
+  if (is_separator (line))
+    {
+      pp->priv->separator_count++;
+
+      if (pp->priv->separator_count == 2)
+        {
+          pp->priv->separator_count = 0;
+          g_warning ("PostProcessorLatexmk: fetch sub-command failed, try again.");
+        }
+
+      goto end;
+    }
+
+  if (pp->priv->separator_count != 1)
+    goto end;
+
+  g_regex_match (regex_sub_command, line, 0, &match_info);
+
+  if (g_match_info_matches (match_info))
+    {
+      LatexilaBuildMsg *msg;
+      gchar *command;
+
+      msg = latexila_build_msg_new ();
+
+      command = g_match_info_fetch_named (match_info, "command");
+      msg->text = g_strdup_printf ("$ %s", command);
+      g_free (command);
+
+      add_sub_message (pp, msg);
+      pp->priv->state = STATE_FETCH_SUB_COMMAND_END;
+    }
+
+  g_match_info_free (match_info);
+
+end:
+  g_free (line);
+}
+
+static void
+latexila_post_processor_latexmk_process_line (LatexilaPostProcessor *post_processor,
+                                              gchar                 *line)
+{
+  LatexilaPostProcessorLatexmk *pp = LATEXILA_POST_PROCESSOR_LATEXMK (post_processor);
+
+  switch (pp->priv->state)
+    {
+    case STATE_FETCH_SUB_TITLE_START:
+    case STATE_FETCH_SUB_TITLE_END:
+      fetch_sub_title (pp, line);
+      break;
+
+    case STATE_FETCH_SUB_COMMAND_START:
+    case STATE_FETCH_SUB_COMMAND_END:
+      fetch_sub_command (pp, line);
+      break;
+
+    default:
+      g_return_if_reached ();
+    }
+}
+
+static const GQueue *
+latexila_post_processor_latexmk_get_messages (LatexilaPostProcessor *post_processor)
+{
+  LatexilaPostProcessorLatexmk *pp = LATEXILA_POST_PROCESSOR_LATEXMK (post_processor);
+
+  return pp->priv->messages;
+}
+
+static void
+latexila_post_processor_latexmk_dispose (GObject *object)
+{
+
+  G_OBJECT_CLASS (latexila_post_processor_latexmk_parent_class)->dispose (object);
+}
+
+static void
+latexila_post_processor_latexmk_finalize (GObject *object)
+{
+  LatexilaPostProcessorLatexmk *pp = LATEXILA_POST_PROCESSOR_LATEXMK (object);
+
+  g_queue_free_full (pp->priv->messages, (GDestroyNotify) latexila_build_msg_free);
+
+  if (pp->priv->last_latex_lines != NULL)
+    g_queue_free_full (pp->priv->last_latex_lines, g_free);
+
+  G_OBJECT_CLASS (latexila_post_processor_latexmk_parent_class)->finalize (object);
+}
+
+static void
+latexila_post_processor_latexmk_class_init (LatexilaPostProcessorLatexmkClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  LatexilaPostProcessorClass *pp_class = LATEXILA_POST_PROCESSOR_CLASS (klass);
+
+  object_class->dispose = latexila_post_processor_latexmk_dispose;
+  object_class->finalize = latexila_post_processor_latexmk_finalize;
+
+  pp_class->start = latexila_post_processor_latexmk_start;
+  pp_class->end = latexila_post_processor_latexmk_end;
+  pp_class->process_line = latexila_post_processor_latexmk_process_line;
+  pp_class->get_messages = latexila_post_processor_latexmk_get_messages;
+}
+
+static void
+latexila_post_processor_latexmk_init (LatexilaPostProcessorLatexmk *pp)
+{
+  pp->priv = latexila_post_processor_latexmk_get_instance_private (pp);
+
+  pp->priv->messages = g_queue_new ();
+  pp->priv->state = STATE_FETCH_SUB_TITLE_START;
+}
+
+/**
+ * latexila_post_processor_latexmk_new:
+ *
+ * Returns: a new #LatexilaPostProcessorLatexmk object.
+ */
+LatexilaPostProcessor *
+latexila_post_processor_latexmk_new (void)
+{
+  return g_object_new (LATEXILA_TYPE_POST_PROCESSOR_LATEXMK, NULL);
+}
diff --git a/src/liblatexila/latexila-post-processor-latexmk.h 
b/src/liblatexila/latexila-post-processor-latexmk.h
new file mode 100644
index 0000000..f30382f
--- /dev/null
+++ b/src/liblatexila/latexila-post-processor-latexmk.h
@@ -0,0 +1,57 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __LATEXILA_POST_PROCESSOR_LATEXMK_H__
+#define __LATEXILA_POST_PROCESSOR_LATEXMK_H__
+
+#include <glib-object.h>
+#include "latexila-post-processor.h"
+#include "latexila-types.h"
+
+G_BEGIN_DECLS
+
+#define LATEXILA_TYPE_POST_PROCESSOR_LATEXMK             (latexila_post_processor_latexmk_get_type ())
+#define LATEXILA_POST_PROCESSOR_LATEXMK(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
LATEXILA_TYPE_POST_PROCESSOR_LATEXMK, LatexilaPostProcessorLatexmk))
+#define LATEXILA_POST_PROCESSOR_LATEXMK_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), 
LATEXILA_TYPE_POST_PROCESSOR_LATEXMK, LatexilaPostProcessorLatexmkClass))
+#define LATEXILA_IS_POST_PROCESSOR_LATEXMK(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
LATEXILA_TYPE_POST_PROCESSOR_LATEXMK))
+#define LATEXILA_IS_POST_PROCESSOR_LATEXMK_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), 
LATEXILA_TYPE_POST_PROCESSOR_LATEXMK))
+#define LATEXILA_POST_PROCESSOR_LATEXMK_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), 
LATEXILA_TYPE_POST_PROCESSOR_LATEXMK, LatexilaPostProcessorLatexmkClass))
+
+typedef struct _LatexilaPostProcessorLatexmkClass   LatexilaPostProcessorLatexmkClass;
+typedef struct _LatexilaPostProcessorLatexmkPrivate LatexilaPostProcessorLatexmkPrivate;
+
+struct _LatexilaPostProcessorLatexmk
+{
+  LatexilaPostProcessor parent;
+
+  LatexilaPostProcessorLatexmkPrivate *priv;
+};
+
+struct _LatexilaPostProcessorLatexmkClass
+{
+  LatexilaPostProcessorClass parent_class;
+};
+
+GType                   latexila_post_processor_latexmk_get_type  (void) G_GNUC_CONST;
+
+LatexilaPostProcessor * latexila_post_processor_latexmk_new       (void);
+
+G_END_DECLS
+
+#endif /* __LATEXILA_POST_PROCESSOR_LATEXMK_H__ */
diff --git a/src/liblatexila/latexila-types.h b/src/liblatexila/latexila-types.h
index 997aa99..6c01249 100644
--- a/src/liblatexila/latexila-types.h
+++ b/src/liblatexila/latexila-types.h
@@ -33,6 +33,7 @@ typedef struct _LatexilaBuildView               LatexilaBuildView;
 typedef struct _LatexilaPostProcessor           LatexilaPostProcessor;
 typedef struct _LatexilaPostProcessorAllOutput  LatexilaPostProcessorAllOutput;
 typedef struct _LatexilaPostProcessorLatex      LatexilaPostProcessorLatex;
+typedef struct _LatexilaPostProcessorLatexmk    LatexilaPostProcessorLatexmk;
 typedef struct _LatexilaSynctex                 LatexilaSynctex;
 
 G_END_DECLS


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