bar'"""
19 |
20 | _, tmp = tempfile.mkstemp(".dconf")
21 | with open(tmp, "w", encoding="utf-8") as file:
22 | file.write(term_cfg)
23 |
24 | kb_inject = f"dconf load /org/gnome/terminal/legacy/keybindings/ < {tmp} "
25 | subprocess.check_output(kb_inject, shell=True)
26 |
27 |
28 | def send_lines(keylist):
29 | """
30 | Press the supplied keys
31 | """
32 | if isinstance(keylist, str):
33 | gui.typewrite(keylist + "\n")
34 | elif isinstance(keylist, list):
35 | cmd = "\n".join(keylist)
36 | send_lines(cmd)
37 | else:
38 | raise TypeError()
39 |
40 |
41 | def snap_from_term():
42 | """
43 | Copy's the text visible on the terminal screen
44 | """
45 | gui.hotkey("ctrl", "|")
46 | gui.hotkey("ctrl", "}")
47 | clipboard_content = pyperclip.paste()
48 | return clipboard_content
49 |
50 |
51 | def find_match(slst):
52 | """
53 | Magic
54 | """
55 | slst = BeautifulSoup(slst, "lxml")
56 | slst = slst.text.split("\n")
57 | # __import__("pprint").pprint(slst)
58 |
59 | if slst[-1][0] == "~" and slst[-1].strip(" ") == "~":
60 | return True
61 | return False
62 |
63 |
64 | def get_term_output(ad_start=0, ad_end=None):
65 | """
66 | Term output Processor for Neorg on nvim
67 | """
68 | buffer_dump = []
69 |
70 | old_clipboard_content = ""
71 | while (clipboard_content := snap_from_term()) != old_clipboard_content:
72 |
73 | old_clipboard_content = clipboard_content
74 | # Copy everything on screen gnome-terminal screen
75 | clipboard_content = clipboard_content.split("\n")
76 | clipboard_content = clipboard_content[1:-3][ad_start:ad_end]
77 | clipboard_content = "\n".join(clipboard_content)
78 |
79 | buffer_dump.append(clipboard_content)
80 | gui.hotkey("pagedown")
81 |
82 | if len(buffer_dump) > 1:
83 | dmp = buffer_dump.pop() if find_match(buffer_dump[-2]) else None
84 | if len(buffer_dump) > 1:
85 | dmp = buffer_dump.pop() if find_match(buffer_dump[-2]) else None
86 | if len(buffer_dump) > 1:
87 | dmp = buffer_dump.pop() if find_match(buffer_dump[-2]) else dmp
88 |
89 | return buffer_dump
90 |
91 |
92 | def post_processor(dump):
93 | """
94 | post processor
95 | """
96 | page = [
97 | "",
98 | "",
99 | f""
100 | f"",
101 | dump,
102 | "
",
103 | "",
104 | "",
105 | "
",
106 | ]
107 | page = "\n".join(page)
108 | return page
109 |
110 |
111 | def export_norg(file_path, out_path=None):
112 | """
113 | Handler
114 | """
115 | out_path = out_path if out_path else file_path.replace("norg", "html")
116 |
117 | # Create a copy of original norg file
118 | copy(file_path, TMP_FILE_PATH)
119 |
120 | # Start the terminal
121 | if TERM_START_WIDTH:
122 | term_start_post = [
123 | f"stty columns {TERM_START_WIDTH}",
124 | f"nvim {TMP_FILE_PATH};exit",
125 | ]
126 | subprocess.run(TERM_START_RAW, check=True)
127 | send_lines(term_start_post)
128 | else:
129 | subprocess.run(TERM_START_CMD, check=True)
130 | gui.sleep(START_DELAY)
131 |
132 | # Conf Features
133 | vim_cmds = [
134 | ":set nocursorcolumn nocursorline colorcolumn=10000 list! norelativenumber nonumber", # clean some clutter
135 | "zR", # open all folds
136 | ":IndentBlanklineDisable",
137 | ]
138 |
139 | excmds = [":Neorg presenter start"]
140 | colcmds = [f":colorscheme {COLORSCHEME} "]
141 | vim_cmds = excmds + vim_cmds if ENABLE_PRESENTER else vim_cmds
142 | vim_cmds = colcmds + vim_cmds if COLORSCHEME else vim_cmds
143 | send_lines(vim_cmds)
144 | gui.sleep(PROCESS_DELAY)
145 |
146 | # post processing
147 | bufcnt = get_term_output() if ENABLE_PRESENTER else get_term_output(0, -2)
148 | if ENABLE_PRESENTER:
149 | pages = [post_processor(page) for page in bufcnt]
150 | document = "\n\n".join(pages)
151 | else:
152 | bufcnt = "\n".join(bufcnt)
153 | document = post_processor(bufcnt)
154 |
155 | with open(out_path, "w", encoding="utf-8") as file:
156 | file.write(document)
157 |
158 | exitcmdlst = [":qall!"]
159 | send_lines(exitcmdlst)
160 | remove(TMP_FILE_PATH)
161 |
162 |
163 | _, TMP_FILE_PATH = tempfile.mkstemp(".norg")
164 | TERM_START_CMD = ["gnome-terminal", "--", "nvim", TMP_FILE_PATH]
165 | TERM_START_RAW = ["gnome-terminal"]
166 | TERM_START_WIDTH = None
167 |
168 | ENABLE_PRESENTER = False
169 | START_DELAY = 1
170 | PROCESS_DELAY = 1
171 | BGCOLOR = "#282C34"
172 | FONT = "Fira Code"
173 | COLORSCHEME = None
174 |
175 | if __name__ == "__main__":
176 | parser = argparse.ArgumentParser()
177 | parser.add_argument("path")
178 | parser.add_argument("--output", default=None, help="path to output")
179 | parser.add_argument("--font", default=FONT, help="font to set for the output")
180 | parser.add_argument("--bgcolor", default=BGCOLOR, help="background color to fill")
181 | parser.add_argument("--colorscheme", default=None, help="specify vim colorscheme")
182 | parser.add_argument("--delay", default=START_DELAY,type=int, help="specify vim startup or load delay")
183 | parser.add_argument(
184 | "--width", type=int, default=TERM_START_WIDTH, help="specify width"
185 | )
186 | parser.add_argument(
187 | "--presenter", action="store_true", help="Enable norg presenter mode"
188 | )
189 | args = parser.parse_args()
190 | inject_keybind()
191 |
192 | ENABLE_PRESENTER = args.presenter
193 | BGCOLOR = args.bgcolor
194 | FONT = args.font
195 | COLORSCHEME = args.colorscheme
196 | TERM_START_WIDTH = args.width
197 | START_DELAY = args.delay
198 |
199 | export_norg(args.path, args.output)
200 | subprocess.check_output("dconf reset -f /org/gnome/terminal/legacy/keybindings/",shell=True)
201 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | pyautogui
2 | pyperclip
3 | beautifulsoup4
4 | lxml
5 |
--------------------------------------------------------------------------------