[geany/geany-plugins] 394a46: Markdown: Fix relative paths (#501)
Matthew Brush
git-noreply at geany.org
Sun Nov 13 17:23:12 UTC 2016
Branch: refs/heads/master
Author: Matthew Brush <mbrush at codebrainz.ca>
Committer: Frank Lanitz <frank at frank.uvena.de>
Date: Sun, 13 Nov 2016 17:23:12 UTC
Commit: 394a4647bf6a640edcb25464370b02c055865055
https://github.com/geany/geany-plugins/commit/394a4647bf6a640edcb25464370b02c055865055
Log Message:
-----------
Markdown: Fix relative paths (#501)
Stop using a phony base URI and create one from the current filename,
with various fallbacks in case the file isn't saved yet or URI encoding
fails.
Closes #500
Modified Paths:
--------------
markdown/src/viewer.c
Modified: markdown/src/viewer.c
30 lines changed, 29 insertions(+), 1 deletions(-)
===================================================================
@@ -23,6 +23,7 @@
#include <string.h>
#include <gtk/gtk.h>
#include <webkit/webkitwebview.h>
+#include <geanyplugin.h>
#ifndef FULL_PRICE
# include <mkdio.h>
#else
@@ -356,7 +357,33 @@ markdown_viewer_update_view(MarkdownViewer *self)
push_scroll_pos(self);
if (html) {
- static const gchar *base_uri = "file://.";
+ gchar *base_path;
+ gchar *base_uri; /* A file URI not a path URI; last component is stripped */
+ GError *error = NULL;
+ GeanyDocument *doc = document_get_current();
+
+ /* If the current document has a known path (ie. is saved), use that,
+ * substituting the file's basename for `index.html`. */
+ if (DOC_VALID(doc) && doc->real_path != NULL) {
+ gchar *base_dir = g_path_get_dirname(doc->real_path);
+ base_path = g_build_filename(base_dir, "index.html", NULL);
+ g_free(base_dir);
+ }
+ /* Otherwise assume use a file `index.html` in the current working directory. */
+ else {
+ gchar *cwd = g_get_current_dir();
+ base_path = g_build_filename(cwd, "index.html", NULL);
+ g_free(cwd);
+ }
+
+ base_uri = g_filename_to_uri(base_path, NULL, &error);
+ if (base_uri == NULL) {
+ g_warning("failed to encode path '%s' as URI: %s", base_path, error->message);
+ g_error_free(error);
+ base_uri = g_strdup("file://./index.html");
+ g_debug("using phony base URI '%s', broken relative paths are likely", base_uri);
+ }
+ g_free(base_path);
/* Connect a signal handler (only needed once) to restore the scroll
* position once the webview is reloaded. */
@@ -369,6 +396,7 @@ markdown_viewer_update_view(MarkdownViewer *self)
webkit_web_view_load_string(WEBKIT_WEB_VIEW(self), html, "text/html",
self->priv->enc, base_uri);
+ g_free(base_uri);
g_free(html);
}
--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).
More information about the Plugins-Commits
mailing list