Added content detection to parser
This commit is contained in:
parent
909a84c16a
commit
e39c0ed7da
@ -213,6 +213,10 @@
|
||||
<property name="can_focus">True</property>
|
||||
<property name="editable">False</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>
|
||||
</object>
|
||||
<packing>
|
||||
|
@ -111,6 +111,7 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
|
||||
G_TYPE_STRING,
|
||||
G_TYPE_POINTER,
|
||||
G_TYPE_POINTER,
|
||||
G_TYPE_STRING,
|
||||
G_TYPE_STRING);
|
||||
mainWindow->entryListFiltered = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(
|
||||
GTK_TREE_MODEL(mainWindow->entryListStore),
|
||||
|
107
src/mdiary.c
107
src/mdiary.c
@ -131,6 +131,42 @@ static gchar *mdiary_get_title_from_string(gchar *string)
|
||||
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)
|
||||
{
|
||||
GRegex *regex;
|
||||
@ -142,12 +178,10 @@ static GList *mdiary_add_tags_from_string(gchar *string)
|
||||
gboolean collected = 0;
|
||||
gchar bak;
|
||||
|
||||
g_print("\n\nREGEX on %s\n", string);
|
||||
regex = g_regex_new("^\\s*Tags: (.*)", 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)) {
|
||||
g_print("T: Matched %d\n", g_regex_get_capture_count(regex));
|
||||
/**
|
||||
* 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;
|
||||
gchar *title = NULL;
|
||||
GList *tagList = NULL;
|
||||
gchar *text = NULL;
|
||||
gboolean in_header = 1;
|
||||
gchar *summary = NULL;
|
||||
GString *text = NULL;
|
||||
guint header_state = 0;
|
||||
|
||||
g_print("Add file: %s\n", filename);
|
||||
|
||||
file = g_file_new_for_path(filename);
|
||||
stream = g_file_read(file, NULL, &err);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
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)) {
|
||||
if (in_header) {
|
||||
if (header_state == 0) {
|
||||
if (!datetime) {
|
||||
datetime = mdiary_get_date_from_string_ext(line, "Date: .*", "");
|
||||
if (datetime)
|
||||
continue;
|
||||
}
|
||||
if (!summary) {
|
||||
summary = mdiary_get_summary_from_string(line);
|
||||
if (summary)
|
||||
continue;
|
||||
}
|
||||
if (!title) {
|
||||
title = mdiary_get_title_from_string(line);
|
||||
if (title)
|
||||
@ -218,31 +258,53 @@ static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListSto
|
||||
if (tagList)
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
if (!text)
|
||||
text = g_strdup("No content found.");
|
||||
if (!title)
|
||||
if (!tagList) {
|
||||
g_warning("PARSER: Could not detect tags in file!\n");
|
||||
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");
|
||||
}
|
||||
|
||||
mdiary_add_entry_to_store(entryListStore,
|
||||
title,
|
||||
datetime,
|
||||
tagList,
|
||||
text);
|
||||
summary,
|
||||
text->str);
|
||||
|
||||
g_input_stream_close(G_INPUT_STREAM(dstream), 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_date_time_unref(datetime);
|
||||
g_object_unref(file);
|
||||
@ -324,25 +386,30 @@ void mdiary_add_entry_to_store(GtkListStore *entryListStore,
|
||||
gchar *title,
|
||||
GDateTime *datetime,
|
||||
GList *tags,
|
||||
gchar *summary,
|
||||
gchar *text)
|
||||
{
|
||||
GtkTreeIter iter;
|
||||
GDateTime *datetime_copy;
|
||||
GList *taglist_copy;
|
||||
gchar *date_text;
|
||||
gchar *taglistString;
|
||||
|
||||
datetime_copy = g_date_time_add(datetime, 0);
|
||||
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_set(entryListStore, &iter,
|
||||
COL_TITLE, title,
|
||||
COL_TITLE, g_strdup(title),
|
||||
COL_DATE_TEXT, date_text,
|
||||
COL_TAGS_TEXT, taglistString,
|
||||
COL_TIMESTAMP, datetime_copy,
|
||||
COL_TAGLIST, NULL, /* TODO: !!!! */
|
||||
COL_TEXT, g_strdup(text),
|
||||
COL_TAGLIST, taglist_copy, /* TODO: Verify that the duplication worked! */
|
||||
COL_SUMMARY, summary, /* Automatically strdupd */
|
||||
COL_TEXT, text, /* Automatically strdupd */
|
||||
-1);
|
||||
g_free(date_text);
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ enum {
|
||||
COL_TAGS_TEXT, /* Textual representation of the tags (auto generated) */
|
||||
COL_TIMESTAMP, /* GDateTime of the entry */
|
||||
COL_TAGLIST, /* GList<gchar *> of the tags */
|
||||
COL_SUMMARY, /* Summary of the entry */
|
||||
COL_TEXT, /* Raw text from the file */
|
||||
COL_COUNT
|
||||
};
|
||||
@ -21,6 +22,7 @@ void mdiary_add_entry_to_store(GtkListStore *entryListStore,
|
||||
gchar *title,
|
||||
GDateTime *datetime,
|
||||
GList *tags,
|
||||
gchar *summary,
|
||||
gchar *text);
|
||||
GDateTime *mdiary_get_date_from_string_ext(gchar *string, gchar *prefix, gchar *suffix);
|
||||
GDateTime *mdiary_get_date_from_string(gchar *string);
|
||||
|
Loading…
Reference in New Issue
Block a user