browser: open editor to view/edit sources & pages

This commit is contained in:
dece 2021-04-16 19:30:33 +02:00
parent bce65a1472
commit 347b5a81cc
4 changed files with 81 additions and 10 deletions

25
BOARD.txt Normal file
View file

@ -0,0 +1,25 @@
TODO DONE
--------------------------------------------------------------------------------
browsing
links
redirections
web links
history
simple caching
simple text files
encodings
bookmarks
view/edit sources
non shit command-line
home page
downloads
media files
identity management
configuration
--------------------------------------------------------------------------------
BACKLOG
margins / centering
pre blocks folding
buffers (tabs)
handle soft-hyphens on wrapping
bug: combining chars reduce lengths

View file

@ -27,16 +27,16 @@ Features
### What works ### What works
- Basic browsing: scrolling, follow links, redirections, Web links. Common basic browsing features work: go to URL, scrolling, follow links,
redirections, page encoding.
### What is planned Bebop also provide these neat features:
- Handle more content types. - History
- Great config options. - Caching
- Identity management with temporary and managed certificates. - Bookmarks: it's just a text file with bindings.
- Buffers (or tabs if you prefer).
- Home page. Check out [this board](BOARD.txt) for what's done and coming next.
- Bookmarks.
### What is not planned for now ### What is not planned for now

View file

@ -4,9 +4,13 @@ import curses
import curses.ascii import curses.ascii
import curses.textpad import curses.textpad
import os import os
import subprocess
import tempfile
from math import inf from math import inf
from bebop.bookmarks import get_bookmarks_document, save_bookmark from bebop.bookmarks import (
get_bookmarks_path, get_bookmarks_document, save_bookmark
)
from bebop.colors import ColorPair, init_colors from bebop.colors import ColorPair, init_colors
from bebop.command_line import CommandLine from bebop.command_line import CommandLine
from bebop.history import History from bebop.history import History
@ -131,6 +135,8 @@ class Browser:
self.open_bookmarks() self.open_bookmarks()
elif char == ord("B"): elif char == ord("B"):
self.add_bookmark() self.add_bookmark()
elif char == ord("e"):
self.edit_page()
elif curses.ascii.isdigit(char): elif curses.ascii.isdigit(char):
self.handle_digit_input(char) self.handle_digit_input(char)
elif char == curses.KEY_MOUSE: elif char == curses.KEY_MOUSE:
@ -421,3 +427,42 @@ class Browser:
if title: if title:
save_bookmark(self.current_url, title) save_bookmark(self.current_url, title)
self.reset_status() self.reset_status()
def open_external_program(self, command):
"""Pauses the curses modes to open an external program."""
curses.nocbreak()
curses.echo()
subprocess.run(command)
curses.noecho()
curses.cbreak()
self.refresh_windows()
def edit_page(self):
"""Open a text editor to edit the page source.
For external pages, the source is written in a temporary file, opened in
its editor of choice and so it's up to the user to save it where she
needs it, if needed. Internal pages, e.g. the bookmarks page, are loaded
directly from their location on disk.
"""
command = ["vi"]
delete_source_after = False
special_pages = {
"bebop://bookmarks": str(get_bookmarks_path())
}
if self.current_url in special_pages:
source_filename = special_pages[self.current_url]
else:
if not self.page_pad.current_page:
return
source = self.page_pad.current_page.source
with tempfile.NamedTemporaryFile("wt", delete=False) as source_file:
source_file.write(source)
source_filename = source_file.name
delete_source_after = True
command.append(source_filename)
self.open_external_program(command)
if delete_source_after:
os.unlink(source_filename)

View file

@ -17,6 +17,7 @@ class Page:
lookup and disambiguation. lookup and disambiguation.
- title: optional page title. - title: optional page title.
""" """
source: str
metalines: list = field(default_factory=list) metalines: list = field(default_factory=list)
links: Links = field(default_factory=Links) links: Links = field(default_factory=Links)
title: str = "" title: str = ""
@ -26,4 +27,4 @@ class Page:
"""Produce a Page from a Gemtext file or string.""" """Produce a Page from a Gemtext file or string."""
elements, links, title = parse_gemtext(gemtext) elements, links, title = parse_gemtext(gemtext)
metalines = generate_metalines(elements, 80) metalines = generate_metalines(elements, 80)
return Page(metalines, links, title) return Page(gemtext, metalines, links, title)