~sotirisp/qute-gemini

c58e136a969531592f8959250526da0cd4957d85 — Sotiris Papatheodorou 8 months ago 609563a
Make consistent use of double quotes
1 files changed, 38 insertions(+), 38 deletions(-)

M qute-gemini
M qute-gemini => qute-gemini +38 -38
@@ 26,13 26,13 @@ def script_dir() -> str:

def qute_url() -> str:
    """Get the URL passed to the script by qutebrowser"""
    return os.environ['QUTE_URL']
    return os.environ["QUTE_URL"]



def qute_fifo() -> str:
    """Get the FIFO or file to write qutebrowser commands to"""
    return os.environ['QUTE_FIFO']
    return os.environ["QUTE_FIFO"]





@@ 112,62 112,62 @@ def gemtext_to_html(gemtext, title, url):
    in_list = False
    lines = ['<?xml version="1.0" encoding="UTF-8"?>',
            '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">',
            '  <head>',
            '    <title>' + title + '</title>',
            '  </head>',
            '  <body>']
            "  <head>",
            "    <title>" + title + "</title>",
            "  </head>",
            "  <body>"]
    # Add an extra newline to ensure list tags are closed properly
    for line in (gemtext + "\n").splitlines():
        # Add the list closing tag
        if not line.startswith('*') and in_list:
            lines.append('    </ul>')
        if not line.startswith("*") and in_list:
            lines.append("    </ul>")
            in_list = False
        # Blank line
        if not line:
            # Ignore
            pass
        # Link
        elif line.startswith('=>'):
        elif line.startswith("=>"):
            l = line[2:].split(None, 1)
            # Use the URL itself as the description if there is none
            if len(l) == 1:
                l.append(l[0])
            # Resolve relative URLs
            l[0] = gemini_absolutise_url(url, l[0])
            lines.append(''.join(['    <p><a href="', l[0], '">', l[1], '</a></p>']))
            lines.append("".join(['    <p><a href="', l[0], '">', l[1], "</a></p>"]))
        # Preformated toggle
        elif line.startswith('```'):
        elif line.startswith("```"):
            if in_pre:
                lines.append('    </pre>')
                lines.append("    </pre>")
            else:
                lines.append('    <pre>')
                lines.append("    <pre>")
            in_pre = not in_pre
        # Preformated
        elif in_pre:
            lines.append(line)
        # Header
        elif line.startswith('###'):
            lines.append('    <h3>' + line[3:].strip() + '</h3>')
        elif line.startswith('##'):
            lines.append('    <h2>' + line[2:].strip() + '</h2>')
        elif line.startswith('#'):
            lines.append('    <h1>' + line[1:].strip() + '</h1>')
        elif line.startswith("###"):
            lines.append("    <h3>" + line[3:].strip() + "</h3>")
        elif line.startswith("##"):
            lines.append("    <h2>" + line[2:].strip() + "</h2>")
        elif line.startswith("#"):
            lines.append("    <h1>" + line[1:].strip() + "</h1>")
        # List
        elif line.startswith('*'):
        elif line.startswith("*"):
            if not in_list:
                lines.append('    <ul>')
                lines.append("    <ul>")
            in_list = True
            lines.append('      <li>' + line[1:].strip() + '</li>')
            lines.append("      <li>" + line[1:].strip() + "</li>")
        # Quote
        elif line.startswith('>'):
            lines.append('    <blockquote>\n      <p>' + line[1:].strip() + '</p>\n    </blockquote>')
        elif line.startswith(">"):
            lines.append("    <blockquote>\n      <p>" + line[1:].strip() + "</p>\n    </blockquote>")
        # Normal text
        else:
            lines.append('    <p>' + line.strip() + '</p>')
    lines.append('')
    lines.append('  </body>\n')
    lines.append('</html>')
    return '\n'.join(lines)
            lines.append("    <p>" + line.strip() + "</p>")
    lines.append("")
    lines.append("  </body>\n")
    lines.append("</html>")
    return "\n".join(lines)





@@ 179,30 179,30 @@ def open_gemini(url: str, open_args: str):
        sys.exit(1)

    # Convert to HTML in a temporary file
    tmpf = tempfile.NamedTemporaryFile('w', suffix='.html', delete=False)
    tmpf = tempfile.NamedTemporaryFile("w", suffix=".html", delete=False)
    tmp_filename = tmpf.name
    tmpf.close()
    with open(tmp_filename, 'w') as f:
    with open(tmp_filename, "w") as f:
        f.write(gemtext_to_html(content, url, url))

    # Open the HTML file in qutebrowser
    with open(qute_fifo(), 'w') as qfifo:
        qfifo.write('open ' + open_args + ' file://' + tmp_filename)
    with open(qute_fifo(), "w") as qfifo:
        qfifo.write("open " + open_args + " file://" + tmp_filename)



def open_other(url: str, open_args: str):
    with open(qute_fifo(), 'w') as qfifo:
        qfifo.write('open ' + open_args + ' ' + url)
    with open(qute_fifo(), "w") as qfifo:
        qfifo.write("open " + open_args + " " + url)



if __name__ == "__main__":
    # Open in the current or a new tab depending on the script name
    if sys.argv[0].endswith('-tab'):
        open_args = '-t'
    if sys.argv[0].endswith("-tab"):
        open_args = "-t"
    else:
        open_args = ''
        open_args = ""

    # Select how to open the URL depending on its scheme
    url = qute_url()