Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown: Allow exporting Markdown as HTML #502

Merged
merged 2 commits into from
Nov 13, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions markdown/src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ PLUGIN_SET_TRANSLATABLE_INFO(LOCALEDIR, GETTEXT_PACKAGE,
/* Global data */
static MarkdownViewer *g_viewer = NULL;
static GtkWidget *g_scrolled_win = NULL;
static GtkWidget *g_export_html = NULL;

/* Forward declarations */
static void update_markdown_viewer(MarkdownViewer *viewer);
static gboolean on_editor_notify(GObject *obj, GeanyEditor *editor, SCNotification *notif, MarkdownViewer *viewer);
static void on_document_signal(GObject *obj, GeanyDocument *doc, MarkdownViewer *viewer);
static void on_document_filetype_set(GObject *obj, GeanyDocument *doc, GeanyFiletype *ft_old, MarkdownViewer *viewer);
static void on_view_pos_notify(GObject *obj, GParamSpec *pspec, MarkdownViewer *viewer);
static void on_export_as_html_activate(GtkMenuItem *item, MarkdownViewer *viewer);

/* Main plugin entry point on plugin load. */
void plugin_init(GeanyData *data)
Expand Down Expand Up @@ -97,6 +99,11 @@ void plugin_init(GeanyData *data)

g_signal_connect(conf, "notify::view-pos", G_CALLBACK(on_view_pos_notify), viewer);

g_export_html = gtk_menu_item_new_with_label(_("Export Markdown as HTML..."));
gtk_menu_shell_append(GTK_MENU_SHELL(data->main_widgets->tools_menu), g_export_html);
g_signal_connect(g_export_html, "activate", G_CALLBACK(on_export_as_html_activate), viewer);
gtk_widget_show(g_export_html);

#define MD_PSC(sig, cb) \
plugin_signal_connect(geany_plugin, NULL, (sig), TRUE, G_CALLBACK(cb), viewer)
/* Geany takes care of disconnecting these for us when the plugin is unloaded,
Expand All @@ -119,6 +126,7 @@ void plugin_init(GeanyData *data)
/* Cleanup resources on plugin unload. */
void plugin_cleanup(void)
{
gtk_widget_destroy(g_export_html);
gtk_widget_destroy(g_scrolled_win);
}

Expand Down Expand Up @@ -162,9 +170,11 @@ update_markdown_viewer(MarkdownViewer *viewer)
gchar *text;
text = (gchar*) scintilla_send_message(doc->editor->sci, SCI_GETCHARACTERPOINTER, 0, 0);
markdown_viewer_set_markdown(viewer, text, doc->encoding);
gtk_widget_set_sensitive(g_export_html, TRUE);
} else {
markdown_viewer_set_markdown(viewer,
_("The current document does not have a Markdown filetype."), "UTF-8");
gtk_widget_set_sensitive(g_export_html, FALSE);
}

markdown_viewer_queue_update(viewer);
Expand Down Expand Up @@ -240,3 +250,47 @@ on_view_pos_notify(GObject *obj, GParamSpec *pspec, MarkdownViewer *viewer)

update_markdown_viewer(viewer);
}

static void on_export_as_html_activate(GtkMenuItem *item, MarkdownViewer *viewer)
{
GtkWidget *dialog;
GtkFileFilter *filter;

g_return_if_fail(DOC_VALID(document_get_current()));

dialog = gtk_file_chooser_dialog_new(_("Save HTML File As"),
GTK_WINDOW(geany_data->main_widgets->window), GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
NULL);
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think multi-selection is disabled by default, and probably irrelevant for save too :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, just being explicit.

gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), "index.html");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rather use basename(DOC_FILENAME(doc)) + ".html"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice, yeah.


filter = gtk_file_filter_new();
gtk_file_filter_set_name(filter, _("HTML Files"));
gtk_file_filter_add_pattern(filter, "*.html");
gtk_file_filter_add_pattern(filter, "*.htm");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe rather use gtk_file_filter_add_mime_type(filter, "text/html");?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a good idea if it does what it sounds like, is there similar for * (All files)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but I'm afraid not

gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);

filter = gtk_file_filter_new();
gtk_file_filter_set_name(filter, _("All Files"));
gtk_file_filter_add_pattern(filter, "*");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);

if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
gchar *fn = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
gchar *html = markdown_viewer_get_html(viewer);
GError *error = NULL;
if (! g_file_set_contents(fn, html, -1, &error)) {
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
_("Failed to export Markdown HTML to file '%s': %s"),
fn, error->message);
g_error_free(error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe re-show the save dialog in case of error? I guess the most likely reason of failure is permission issue/invalid path, so I imagine the general action would be trying again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

}
g_free(html);
g_free(fn);
}

gtk_widget_destroy(dialog);
}
2 changes: 1 addition & 1 deletion markdown/src/viewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ on_webview_load_status_notify(WebKitWebView *view, GParamSpec *pspec,
}
}

static gchar *
gchar *
markdown_viewer_get_html(MarkdownViewer *self)
{
gchar *md_as_html, *html = NULL;
Expand Down
1 change: 1 addition & 0 deletions markdown/src/viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ GtkWidget *markdown_viewer_new(MarkdownConfig *conf);
void markdown_viewer_set_markdown(MarkdownViewer *self, const gchar *text,
const gchar *encoding);
void markdown_viewer_queue_update(MarkdownViewer *self);
gchar *markdown_viewer_get_html(MarkdownViewer *self);

G_END_DECLS

Expand Down