8d2e6994825f3b1ce5a1e004fda436f076e46e6f — Sotiris Papatheodorou 8 months ago 51390e4
Show original URL, status code and meta in pages
1 files changed, 20 insertions(+), 13 deletions(-)

M qute-gemini
M qute-gemini => qute-gemini +20 -13
@@ 94,13 94,12 @@ def gemini_absolutise_url(base: str, relative: str) -> str:
    return relative

def gemini_fetch_url(url: str) -> Tuple[str, str]:
def gemini_fetch_url(url: str) -> Tuple[str, str, str, str, str]:
    """Fetch a Gemini URL and return the content as a string.

    url: URL with gemini:// or no scheme.
    Returns 2 strings:
      On success returns the content and an empty error message.
      On failure returns an empty content and an error message.
    Returns 4 strings: the content, the URL the content was fetched from, the
      Gemini status code, the value of the meta field and an error message.

    Adapted from gcat: https://github.com/aaronjanse/gcat

@@ 163,22 162,25 @@ def gemini_fetch_url(url: str) -> Tuple[str, str]:
            except KeyError:
                error_msg = "The server sent back something weird."
        error_msg = error_msg.replace("META", meta)
    return content, error_msg
    return content, url, status, meta, error_msg

def gemtext_to_html(gemtext: str, title: str, url: str) -> str:
def gemtext_to_html(gemtext: str, url: str, original_url: str,
                    status: str, meta: str) -> str:
    """Convert gemtext to HTML.

    title: Used as the document title.
    url:   The URL the gemtext was received from. Used to resolve relative URLs
           in the gemtext content.
    url:    The URL the gemtext was received from. Used to resolve relative
            URLs in the gemtext content.
    status: The Gemini status code returned by the server.
    meta:   The meta returned by the server.
    Returns the HTML representation as a string.
    # Accumulate converted gemtext lines
    lines = ['<?xml version="1.0" encoding="UTF-8"?>',
            '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">',
            "\t\t<title>" + title + "</title>",
            "\t\t<title>" + html.escape(url) + "</title>",

@@ 238,15 240,20 @@ def gemtext_to_html(gemtext: str, title: str, url: str) -> str:
            lines.append("\t\t<p>" + html.escape(line.strip()) + "</p>")
    original_url_html = '<a href="' + url + '">' + html.escape(url) + "</a>"
    url_html = '<a href="' + url + '">' + html.escape(url) + "</a>"
    original_url_html = '<a href="' + original_url + '">' + html.escape(url) + "</a>"
    lines.append("\t\t\tContent from " + original_url_html)
    lines.append("\t\t\tContent from " + url_html)
    lines.append("\t\t\t<dt>Original URL</dt>")
    lines.append("\t\t\t<dd>" + original_url_html + "</dd>")
    lines.append("\t\t\t<dd>" + status + "</dd>")
    lines.append("\t\t\t<dd>" + meta + "</dd>")
    lines.append("\t\t\t<dt>Fetched by</dt>")
    lines.append('\t\t\t<dd><a href="https://git.sr.ht/~sotirisp/qute-gemini">qute-gemini ' + str(_version) + "</a></dd>")

@@ 287,7 294,7 @@ def qute_error_page(url: str, description: str) -> str:
def open_gemini(url: str, open_args: str) -> None:
    """Open Gemini URL in qutebrowser."""
    # Get the Gemini content
    content, error_msg = gemini_fetch_url(url)
    content, content_url, status, meta, error_msg = gemini_fetch_url(url)
    if error_msg:
        open_url = qute_error_page(url, error_msg)

@@ 296,7 303,7 @@ def open_gemini(url: str, open_args: str) -> None:
        tmp_filename = tmpf.name
        with open(tmp_filename, "w") as f:
            f.write(gemtext_to_html(content, url, url))
            f.write(gemtext_to_html(content, content_url, url, status, meta))
        open_url = " file://" + tmp_filename

    # Open the HTML file in qutebrowser