#!/usr/bin/python # zimtable.py # # PURPOSE # Add an empty Markdown-formatted table to the end of a Zim # page. # # NOTES # 1. The name of the temporary Zim page file must be passed # as the (only) command-line argument. # 2. This program will prompt for the number of rows and columns, # and the column width in tabs, and insert a pipe table # with those dimensions, plus one row for headers. # 3. The separator line between headers and table cells # assumes 6 characters per tab. # # AUTHOR # Dreas Nielsen (RDN) # # COPYRIGHT and LICENSE # Copyright (c) 2015, Dreas Nielsen # License: GPL3 # # HISTORY # Date Remarks # ---------- ----------------------------------------------------- # 2015-05-13 Created without GUI to prompt for table size. RDN. # 2015-05-15 Added GUI. RDN. # ===================================================================== _vdate = "2013-05-15" _version = "1.0" import sys import os import Tkinter as tk import ttk if len(sys.argv) != 2: sys.stderr.write("You must provide the temporary Zim page file name as a command-line argument.") sys.exit(1) zimpage = sys.argv[1] if not os.path.exists(zimpage): sys.stderr.write("The file %s does not exist." % zimpage) sys.exit(1) def add_pipe_table(fn, rows, cols, colwidth=3): f = open(fn, "a") tabs = '\t' * colwidth sep = '-' * (6 * colwidth - 1) datarow = "|" + (cols) * ("%s%s" % (tabs, "|")) + '\n' seprow = "|" + (cols) * ("%s%s" % (sep, "|")) + '\n' f.write('\n') f.write(datarow) f.write(seprow) for r in range(rows): f.write(datarow) f.close() def cancel_table(*args): ui.destroy() def make_table(*args): add_pipe_table(zimpage, int(row_val.get()), int(col_val.get()), int(width_val.get())) ui.destroy() ui = tk.Tk() ui.title("Create Markdown Table in Zim") # Frames optframe = ttk.Frame(master=ui, padding="4 4 4 4") btnframe = ttk.Frame(master=ui, padding="4 4 4 4") optframe.grid(column=0, row=1, sticky=tk.EW) btnframe.grid(column=0, row=2, sticky=tk.EW) # Input frame contents row_label = ttk.Label(master=optframe, text="Rows:") row_val = tk.StringVar(optframe, value=10) row_inp = tk.Spinbox(optframe, from_=1, to=50, textvariable=row_val) col_label = ttk.Label(master=optframe, text="Columns:") col_val = tk.StringVar(optframe, value=4) col_inp = tk.Spinbox(optframe, from_=1, to=20, textvariable=col_val) width_label = ttk.Label(master=optframe, text="Column width in tabs:") width_val = tk.StringVar(optframe, value=3) width_inp = tk.Spinbox(optframe, from_=1, to=5, textvariable=width_val) row_label.grid(column=0, row=0, sticky=tk.E) row_inp.grid(column=1, row=0, sticky=tk.W) col_label.grid(column=0, row=1, sticky=tk.E) col_inp.grid(column=1, row=1, sticky=tk.W) width_label.grid(column=0, row=2, sticky=tk.E) width_inp.grid(column=1, row=2, sticky=tk.W) # Button frame contents cancel_button = ttk.Button(btnframe, text="Cancel", command=cancel_table) cancel_button.grid(column=0, row=0, sticky=tk.E, padx=3) maketable_button = ttk.Button(btnframe, text="Make Table", command=make_table) maketable_button.grid(column=1, row=0, sticky=tk.E, padx=3) ui.bind("", cancel_table) ui.bind(" ", make_table) ui.eval('tk::PlaceWindow %s center' % ui.winfo_pathname(ui.winfo_id())) ui.mainloop()
Friday, May 15, 2015
A Markdown Table Generator for Zim
Zim's interface for table creation requires tables to be built up row by row. If you know how many rows you need in a table, Zim's 'Custom Tools' feature can be used to simplify the process of creating a table. Following is a Python script that can be used to create an empty Markdown table, with the desired number of rows and columns, at the bottom of a Zim wiki page. A custom tool should be created in Zim that calls this script with the "%f" parameter as the sole command-line argument. After this script is run you will see the table in the page as Markdown text; type Ctrl-R to reload the page, and Zim will format it like a table created with Zim's own table-creation tool.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment