Created
November 1, 2019 03:57
-
-
Save EvanTheB/f0e5d47d9fb5c3258dd059885d0a9dfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import os | |
import json | |
import subprocess | |
from itertools import tee, filterfalse | |
import markdown_link_extractor | |
from picotui.screen import Screen | |
from picotui.widgets import * | |
from picotui.defs import * | |
from picotui.dialogs import * | |
def partition(pred, iterable): | |
'Use a predicate to partition entries into false entries and true entries' | |
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9 | |
t1, t2 = tee(iterable) | |
return list(filterfalse(pred, t1)), list(filter(pred, t2)) | |
def do_tui(external_links): | |
changes = {} | |
# # This routine is called on screen resize | |
# def screen_resize(s): | |
# # Widgets in dialog store absolute screen coordinates, so | |
# # we need to recreate it from scratch for new dimensions. | |
# d = create_dialog() | |
# screen_redraw(s, d) | |
# def create_dialog(): | |
# d = Dialog(1, 1, 80, 16) | |
# w_listbox = WListBox(79, 15, ["%s" % i for i in external_links]) | |
# d.add(0, 0, w_listbox) | |
# return d | |
# # This routine is called to redraw screen | |
# def screen_redraw(s, d): | |
# # s.attr_color(C_WHITE, C_BLUE) | |
# s.cls() | |
# # s.attr_reset() | |
# d.redraw() | |
try: | |
s = Screen() | |
width, height = Screen.screen_size() | |
s.init_tty() | |
s.enable_mouse() | |
# s.attr_color(C_WHITE, C_BLUE) | |
while True: | |
s.cls() | |
# d = DMultiEntry(80, 25, ["%s" % i for i in external_links]) | |
# res = d.result() | |
s.attr_reset() | |
d = Dialog(0, 0, width, height) | |
w_listbox = WListBox( | |
width - 1, | |
height - 1, | |
["%s" % i for i in external_links] | |
) | |
w_listbox.finish_dialog = 999 | |
d.add(1, 1, w_listbox) | |
b = WButton(8, "OK") | |
b.finish_dialog = ACTION_OK | |
d.add(0, 0, b) | |
# b = WButton(8, "DONE") | |
# b.finish_dialog = ACTION_CANCEL | |
# d.add(10, 0, b) | |
# screen_redraw(Screen) | |
# Screen.set_screen_redraw(screen_redraw) | |
# s.set_screen_resize(screen_resize) | |
res = d.loop() | |
if res == 999: | |
link_to_change_index = w_listbox.get() | |
d = DTextEntry( | |
25, | |
"", | |
title=external_links[link_to_change_index] | |
) | |
res = d.result() | |
changes[external_links[link_to_change_index]] = res | |
del external_links[link_to_change_index] | |
elif res == 1000: | |
s.cls() | |
return changes | |
else: | |
raise Exception | |
finally: | |
s.goto(0, 50) | |
s.cursor(True) | |
s.disable_mouse() | |
s.deinit_tty() | |
def do_tui2(external_links): | |
from prompt_toolkit.formatted_text import HTML | |
from prompt_toolkit.shortcuts import radiolist_dialog | |
result = radiolist_dialog( | |
values=["%s" % i for i in external_links], | |
title='Radiolist dialog example', | |
text='Please select a color:' | |
).run() | |
print('Result = {}'.format(result)) | |
def do_tui3(external_links): | |
from asciimatics.screen import Screen | |
def demo(screen): | |
while True: | |
screen.print_at( | |
'Hello world!', | |
randint(0, | |
screen.width), | |
randint(0, | |
screen.height), | |
colour=randint(0, | |
screen.colours - 1), | |
bg=randint(0, | |
screen.colours - 1) | |
) | |
ev = screen.get_key() | |
if ev in (ord('Q'), ord('q')): | |
return | |
screen.refresh() | |
Screen.wrapper(demo) | |
def check_local_link(curdir, md_link): | |
# leading hash means this is an internal doc link? | |
if not md_link.startswith("#"): | |
# i guess leading / means nothing? | |
if md_link.startswith("/"): | |
md_link = md_link[1:] | |
# cant check heading # links yet | |
if not os.path.exists(os.path.join(curdir, md_link.split("#")[0])): | |
print("bad", md_link) | |
# grep -ho 'http\?://[^][:space:]`)}"]*' *.md | sort | uniq | xargs google-chrome | |
def main(): | |
try: | |
all_changes = json.load(open('link_fixes.json')) | |
except FileNotFoundError as e: | |
all_changes = {} | |
for docfile in sys.argv[1:]: | |
if os.path.exists(docfile + ".done"): | |
continue | |
curdir = os.path.dirname(docfile) | |
doc = open(docfile).read() | |
for bef, aft in all_changes.items(): | |
doc = doc.replace(bef, aft) | |
links = set(markdown_link_extractor.getlinks(doc)) | |
md_links, external_links = partition(lambda l: l.startswith("http"), links) | |
print(docfile) | |
for md_link in md_links: | |
check_local_link(curdir, md_link) | |
input() | |
subprocess.run(['google-chrome', '--new-window'] + external_links) | |
changes = do_tui([l.split("#")[0] for l in external_links]) | |
for md_link in filter(lambda l: not l.startswith("http"), changes.values()): | |
check_local_link(curdir, md_link) | |
all_changes.update(changes) | |
json.dump(all_changes, open('link_fixes.json', 'w')) | |
for bef, aft in changes.items(): | |
doc = doc.replace(bef, aft) | |
open(docfile, 'w').write(doc) | |
open(docfile + ".done", 'w').write("") | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment