[gnome-notes/117-notes-synced-via-nextcloud-are-all-bold: 2/2] note-obj: Fix html from text



commit c9b857ec8f3741d218cec42f0ce59e8607c7c429
Author: Isaque Galdino <igaldino gmail com>
Date:   Fri Feb 8 12:40:10 2019 -0200

    note-obj: Fix html from text
    
    Application was replacing all "\n" character with a "<br/>" tab element,
    when creating html from text.
    
    This change fixes that, wrapping every text line up in a "<div></div>"
    pair tag element, taking care to not do that for the first line, which
    is the note title.

 src/libbiji/biji-note-obj.c | 55 ++++++++++++++++++++++++++++++++-------------
 1 file changed, 39 insertions(+), 16 deletions(-)
---
diff --git a/src/libbiji/biji-note-obj.c b/src/libbiji/biji-note-obj.c
index b84e368..bc2242b 100644
--- a/src/libbiji/biji-note-obj.c
+++ b/src/libbiji/biji-note-obj.c
@@ -815,28 +815,51 @@ biji_note_obj_set_create_date (BijiNoteObj *note, gint64 time)
 gchar *
 html_from_plain_text (const gchar *content)
 {
-  gchar *escaped, *retval;
+  g_auto(GStrv)  lines   = NULL;
+  char          *body    = NULL;
+  char          *retval  = NULL;
+  char          *aux     = NULL;
 
-  if (content == NULL)
-    content = "";
-
-  escaped = biji_str_mass_replace (content,
-                                "&", "&amp;",
-                                "<", "&lt;",
-                                ">", "&gt;",
-                                "\n", "<br/>",
-                                NULL);
+  if (content)
+    {
+      aux = biji_str_mass_replace (content, "&", "&amp;", "<", "&lt;", ">", "&gt;", NULL);
+      lines = g_strsplit (aux, "\n", -1);
+      g_free (aux);
+
+      if (lines)
+        {
+          body = g_strdup (lines[0]);
+          for (int i = 1; lines[i]; i++)
+            {
+              aux = body;
+              if (g_strcmp0 (lines[i], ""))
+                body = g_strconcat (aux, "<div>", lines[i], "</div>", NULL);
+              else
+                body = g_strconcat (aux, "<div><br/></div>", NULL);
+              g_free (aux);
+            }
+        }
+      else
+        {
+          body = g_strdup ("");
+        }
+    }
+  else
+    {
+      body = g_strdup ("");
+    }
 
   retval = g_strconcat ("<html xmlns=\"http://www.w3.org/1999/xhtml\";>",
                         "<head>",
-                        "<link rel='stylesheet' href='Default.css' type='text/css'/>",
-                        "<script language='javascript' src='bijiben.js'></script>"
+                        "<link rel=\"stylesheet\" href=\"Default.css\" type=\"text/css\"/>",
+                        "<script language=\"javascript\" src=\"bijiben.js\"/>"
                         "</head>",
-                        "<body contenteditable='true' id='editable'>",
-                        escaped,
-                        "</body></html>", NULL);
+                        "<body contenteditable=\"true\" id=\"editable\">",
+                        body,
+                        "</body></html>",
+                        NULL);
 
-  g_free (escaped);
+  g_free (body);
   return retval;
 }
 


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