Added content detection to parser

This commit is contained in:
Markus Koch 2017-02-04 17:01:09 +01:00
parent 909a84c16a
commit e39c0ed7da
4 changed files with 95 additions and 21 deletions

View File

@ -213,6 +213,10 @@
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="editable">False</property> <property name="editable">False</property>
<property name="wrap_mode">word</property> <property name="wrap_mode">word</property>
<property name="left_margin">4</property>
<property name="right_margin">4</property>
<property name="top_margin">4</property>
<property name="bottom_margin">4</property>
<property name="buffer">entryTextBuffer</property> <property name="buffer">entryTextBuffer</property>
</object> </object>
<packing> <packing>

View File

@ -111,6 +111,7 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_POINTER, G_TYPE_POINTER,
G_TYPE_POINTER, G_TYPE_POINTER,
G_TYPE_STRING,
G_TYPE_STRING); G_TYPE_STRING);
mainWindow->entryListFiltered = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new( mainWindow->entryListFiltered = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(
GTK_TREE_MODEL(mainWindow->entryListStore), GTK_TREE_MODEL(mainWindow->entryListStore),

View File

@ -131,6 +131,42 @@ static gchar *mdiary_get_title_from_string(gchar *string)
return ret; return ret;
} }
static gchar *mdiary_get_summary_from_string(gchar *string)
{
GRegex *regex;
GMatchInfo *match_info;
gchar *ret = NULL;
regex = g_regex_new("^\\s*Summary: (.*)", G_REGEX_RAW, 0, NULL);
if (g_regex_match(regex, string, 0, &match_info) &&
g_regex_get_capture_count(regex) > 0 &&
g_match_info_matches(match_info)) {
ret = g_strdup(g_match_info_fetch(match_info, 1));
}
g_regex_unref(regex);
return ret;
}
static gboolean mdiary_is_empty_line(gchar *string)
{
GRegex *regex;
GMatchInfo *match_info;
gchar *ret = 0;
regex = g_regex_new("^\\s*$", G_REGEX_RAW, 0, NULL);
if (g_regex_match(regex, string, 0, &match_info) &&
g_match_info_matches(match_info)) {
ret = 1;
}
g_regex_unref(regex);
return ret;
}
static GList *mdiary_add_tags_from_string(gchar *string) static GList *mdiary_add_tags_from_string(gchar *string)
{ {
GRegex *regex; GRegex *regex;
@ -142,12 +178,10 @@ static GList *mdiary_add_tags_from_string(gchar *string)
gboolean collected = 0; gboolean collected = 0;
gchar bak; gchar bak;
g_print("\n\nREGEX on %s\n", string);
regex = g_regex_new("^\\s*Tags: (.*)", G_REGEX_RAW, 0, NULL); regex = g_regex_new("^\\s*Tags: (.*)", G_REGEX_RAW, 0, NULL);
if (g_regex_match(regex, string, 0, &match_info) && if (g_regex_match(regex, string, 0, &match_info) &&
g_regex_get_capture_count(regex) > 0 && g_regex_get_capture_count(regex) > 0 &&
g_match_info_matches(match_info)) { g_match_info_matches(match_info)) {
g_print("T: Matched %d\n", g_regex_get_capture_count(regex));
/** /**
* TODO: This function should be rewritten to fully use GRegex... * TODO: This function should be rewritten to fully use GRegex...
*/ */
@ -187,27 +221,33 @@ static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListSto
GDateTime *datetime = NULL; GDateTime *datetime = NULL;
gchar *title = NULL; gchar *title = NULL;
GList *tagList = NULL; GList *tagList = NULL;
gchar *text = NULL; gchar *summary = NULL;
gboolean in_header = 1; GString *text = NULL;
guint header_state = 0;
g_print("Add file: %s\n", filename); g_print("Add file: %s\n", filename);
file = g_file_new_for_path(filename); file = g_file_new_for_path(filename);
stream = g_file_read(file, NULL, &err); stream = g_file_read(file, NULL, &err);
if (err != NULL) { if (err != NULL) {
g_error("Could not open %s for reading: %s\n", filename, err->message); g_error("PARSER: Could not open %s for reading: %s\n", filename, err->message);
g_error_free(err); g_error_free(err);
return; return;
} }
dstream = g_data_input_stream_new(G_INPUT_STREAM(stream)); dstream = g_data_input_stream_new(G_INPUT_STREAM(stream));
while (line = g_data_input_stream_read_line(G_DATA_INPUT_STREAM(dstream), NULL, NULL, NULL)) { while (line = g_data_input_stream_read_line(G_DATA_INPUT_STREAM(dstream), NULL, NULL, NULL)) {
if (in_header) { if (header_state == 0) {
if (!datetime) { if (!datetime) {
datetime = mdiary_get_date_from_string_ext(line, "Date: .*", ""); datetime = mdiary_get_date_from_string_ext(line, "Date: .*", "");
if (datetime) if (datetime)
continue; continue;
} }
if (!summary) {
summary = mdiary_get_summary_from_string(line);
if (summary)
continue;
}
if (!title) { if (!title) {
title = mdiary_get_title_from_string(line); title = mdiary_get_title_from_string(line);
if (title) if (title)
@ -218,31 +258,53 @@ static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListSto
if (tagList) if (tagList)
continue; continue;
} }
header_state = mdiary_is_empty_line(line);
} else if (header_state == 1) {
header_state += !mdiary_is_empty_line(line);
}
if (header_state == 2) {
if (text) {
text = g_string_append(text, "\n");
text = g_string_append(text, line);
} else {
text = g_string_new(line);
}
} }
} }
if (!tagList) {
tagList = g_list_append(tagList, "untagged");
tagList = g_list_append(tagList, "untagged");
}
if (!datetime) { if (!datetime) {
g_error("Could not detect date in file!\n"); g_warning("PARSER: Could not detect date in file!\n");
datetime = g_date_time_new_from_unix_local(0); datetime = g_date_time_new_from_unix_local(0);
} }
if (!text) if (!tagList) {
text = g_strdup("No content found."); g_warning("PARSER: Could not detect tags in file!\n");
if (!title) tagList = g_list_append(tagList, "untagged");
}
if (!text) {
g_warning("PARSER: Could not find any text in file!\n");
text = g_string_new("No content found.");
}
if (!summary) {
g_warning("PARSER: Could not detect summary in file!\n");
summary = g_strdup("No summary found.");
}
if (!title) {
g_warning("PARSER: Could not detect title in file!\n");
title = g_strdup("Untitled"); title = g_strdup("Untitled");
}
mdiary_add_entry_to_store(entryListStore, mdiary_add_entry_to_store(entryListStore,
title, title,
datetime, datetime,
tagList, tagList,
text); summary,
text->str);
g_input_stream_close(G_INPUT_STREAM(dstream), NULL, NULL); g_input_stream_close(G_INPUT_STREAM(dstream), NULL, NULL);
g_input_stream_close(G_INPUT_STREAM(stream), NULL, NULL); g_input_stream_close(G_INPUT_STREAM(stream), NULL, NULL);
g_free(text); g_string_free(text, 0);
g_free(summary);
g_free(title); g_free(title);
g_date_time_unref(datetime); g_date_time_unref(datetime);
g_object_unref(file); g_object_unref(file);
@ -324,25 +386,30 @@ void mdiary_add_entry_to_store(GtkListStore *entryListStore,
gchar *title, gchar *title,
GDateTime *datetime, GDateTime *datetime,
GList *tags, GList *tags,
gchar *summary,
gchar *text) gchar *text)
{ {
GtkTreeIter iter; GtkTreeIter iter;
GDateTime *datetime_copy; GDateTime *datetime_copy;
GList *taglist_copy;
gchar *date_text; gchar *date_text;
gchar *taglistString; gchar *taglistString;
datetime_copy = g_date_time_add(datetime, 0); datetime_copy = g_date_time_add(datetime, 0);
date_text = g_date_time_format(datetime_copy, "%A, %e %B %Y %R"); date_text = g_date_time_format(datetime_copy, "%A, %e %B %Y %R");
taglistString = mdiary_taglist_to_string(tags); taglist_copy = g_list_copy_deep(tags, (GCopyFunc) g_strdup, NULL);
taglistString = mdiary_taglist_to_string(taglist_copy);
gtk_list_store_append(entryListStore, &iter); gtk_list_store_append(entryListStore, &iter);
gtk_list_store_set(entryListStore, &iter, gtk_list_store_set(entryListStore, &iter,
COL_TITLE, title, COL_TITLE, g_strdup(title),
COL_DATE_TEXT, date_text, COL_DATE_TEXT, date_text,
COL_TAGS_TEXT, taglistString, COL_TAGS_TEXT, taglistString,
COL_TIMESTAMP, datetime_copy, COL_TIMESTAMP, datetime_copy,
COL_TAGLIST, NULL, /* TODO: !!!! */ COL_TAGLIST, taglist_copy, /* TODO: Verify that the duplication worked! */
COL_TEXT, g_strdup(text), COL_SUMMARY, summary, /* Automatically strdupd */
COL_TEXT, text, /* Automatically strdupd */
-1); -1);
g_free(date_text); g_free(date_text);
} }

View File

@ -11,6 +11,7 @@ enum {
COL_TAGS_TEXT, /* Textual representation of the tags (auto generated) */ COL_TAGS_TEXT, /* Textual representation of the tags (auto generated) */
COL_TIMESTAMP, /* GDateTime of the entry */ COL_TIMESTAMP, /* GDateTime of the entry */
COL_TAGLIST, /* GList<gchar *> of the tags */ COL_TAGLIST, /* GList<gchar *> of the tags */
COL_SUMMARY, /* Summary of the entry */
COL_TEXT, /* Raw text from the file */ COL_TEXT, /* Raw text from the file */
COL_COUNT COL_COUNT
}; };
@ -21,6 +22,7 @@ void mdiary_add_entry_to_store(GtkListStore *entryListStore,
gchar *title, gchar *title,
GDateTime *datetime, GDateTime *datetime,
GList *tags, GList *tags,
gchar *summary,
gchar *text); gchar *text);
GDateTime *mdiary_get_date_from_string_ext(gchar *string, gchar *prefix, gchar *suffix); GDateTime *mdiary_get_date_from_string_ext(gchar *string, gchar *prefix, gchar *suffix);
GDateTime *mdiary_get_date_from_string(gchar *string); GDateTime *mdiary_get_date_from_string(gchar *string);