|
27 | 27 | from gui_tools.idlelib_clone import tooltip
|
28 | 28 | from gui_tools.scrollable_frame import VerticalScrolledFrame
|
29 | 29 | from gui_tools.clickable_label import ClickableLabel
|
| 30 | +from gui_tools import download_dialog |
30 | 31 | from threading import Thread
|
31 | 32 | from pathlib import Path
|
32 | 33 | import traceback
|
33 | 34 | import json
|
34 | 35 | from webbrowser import open as open_application
|
| 36 | +from markdown import markdown as markdown_to_html |
35 | 37 | from pathlib import Path
|
36 | 38 | from project_tools import drives, os_detect, project
|
37 | 39 | from typing import Union, Any, Callable
|
@@ -141,6 +143,64 @@ def add_tooltip(self, widget: tk.Widget, text: str) -> None:
|
141 | 143 | :return: None.
|
142 | 144 | """
|
143 | 145 | 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) |
144 | 204 |
|
145 | 205 | def create_config(self) -> None:
|
146 | 206 | """
|
@@ -589,6 +649,8 @@ def create_help_menu(self) -> None:
|
589 | 649 | self.help_menu.add_command(label="Open logs", command=lambda: open_application(str(Path.cwd() / "log.log")), underline=5)
|
590 | 650 | self.help_menu.add_separator()
|
591 | 651 | # TODO: Implement opening the README.md
|
| 652 | + # TODO: Bind to F1 |
| 653 | + # TODO: Add checkbutton on whether to convert to HTML or not |
592 | 654 | self.help_menu.add_command(label="Open README.md", state=tk.DISABLED, underline=5)
|
593 | 655 | # TODO: Implement opening the project on GitHub
|
594 | 656 | self.help_menu.add_command(label="Open project on GitHub", state=tk.DISABLED, underline=5)
|
|
0 commit comments