Skip to content

Commit e71b8bb

Browse files
Add functions to open markdown and update requirements.txt
1 parent cc3cf30 commit e71b8bb

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

gui.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
from gui_tools.idlelib_clone import tooltip
2828
from gui_tools.scrollable_frame import VerticalScrolledFrame
2929
from gui_tools.clickable_label import ClickableLabel
30+
from gui_tools import download_dialog
3031
from threading import Thread
3132
from pathlib import Path
3233
import traceback
3334
import json
3435
from webbrowser import open as open_application
36+
from markdown import markdown as markdown_to_html
3537
from pathlib import Path
3638
from project_tools import drives, os_detect, project
3739
from typing import Union, Any, Callable
@@ -141,6 +143,64 @@ def add_tooltip(self, widget: tk.Widget, text: str) -> None:
141143
:return: None.
142144
"""
143145
tooltip.Hovertip(anchor_widget=widget, text=text)
146+
147+
def open_file(self, path: Union[Path, str], download_url: str = None) -> None:
148+
"""
149+
Open a file or a web page.
150+
151+
:param path: A string or a path representing the web page or the path of the file/directory.
152+
:param download_url: If a file, the link to where we can download the file if it is missing.
153+
:return: None.
154+
"""
155+
logger.debug(f"Opening {repr(path)}...")
156+
if isinstance(path, Path):
157+
if path.exists():
158+
open_application(str(path))
159+
else:
160+
mbox.showerror("CircuitPython Project Manager: ERROR!",
161+
"Oh no! An error occurred while opening this file!\n"
162+
f"The file {repr(path)} does not exist!")
163+
if download_url and mbox.askokcancel("CircuitPython Bundle Manager: Confirm",
164+
"It looks like this file is available on GitHub!\n"
165+
"Would you like to download it?"):
166+
if download_dialog.download(master=self, url=download_url, path=path,
167+
show_traceback=self.show_traceback()):
168+
open_application(str(path))
169+
else:
170+
open_application(path)
171+
172+
def open_markdown(self, path: Union[str, Path], convert_to_html: bool = True, download_url: str = None) -> None:
173+
"""
174+
Open a file or a web page.
175+
176+
:param path: A string or a path to the markdown file.
177+
:param convert_to_html: A bool on whether to convert the markdown to HTML or not.
178+
:param download_url: If a file, the link to where we can download the file if it is missing.
179+
:return: None.
180+
"""
181+
logger.debug(f"Opening markdown file {repr(path)}...")
182+
if isinstance(path, Path):
183+
path = Path(path)
184+
if path.exists():
185+
if convert_to_html:
186+
logger.debug(f"Converting markdown to HTML...")
187+
html_path = Path.cwd() / (path.stem + ".html")
188+
html_path.write_text(markdown_to_html(text=path.read_text(), extensions=["pymdownx.tilde"]))
189+
logger.debug(f"Opening HTML in browser...")
190+
open_application(url=html_path.as_uri())
191+
else:
192+
logger.debug(f"Opening {repr(path)} as markdown!")
193+
open_application(str(path))
194+
else:
195+
mbox.showerror("CircuitPython Project Manager: ERROR!",
196+
"Oh no! An error occurred while opening this file!\n"
197+
f"The file {repr(path)} does not exist!")
198+
if download_url and mbox.askokcancel("CircuitPython Bundle Manager: Confirm",
199+
"It looks like this file is available on GitHub!\n"
200+
"Would you like to download it?"):
201+
if download_dialog.download(master=self, url=download_url, path=path,
202+
show_traceback=self.show_traceback()):
203+
self.open_markdown(path=path)
144204

145205
def create_config(self) -> None:
146206
"""
@@ -589,6 +649,8 @@ def create_help_menu(self) -> None:
589649
self.help_menu.add_command(label="Open logs", command=lambda: open_application(str(Path.cwd() / "log.log")), underline=5)
590650
self.help_menu.add_separator()
591651
# TODO: Implement opening the README.md
652+
# TODO: Bind to F1
653+
# TODO: Add checkbutton on whether to convert to HTML or not
592654
self.help_menu.add_command(label="Open README.md", state=tk.DISABLED, underline=5)
593655
# TODO: Implement opening the project on GitHub
594656
self.help_menu.add_command(label="Open project on GitHub", state=tk.DISABLED, underline=5)

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests
2+
markdown
3+
pymdown-extensions

0 commit comments

Comments
 (0)