Skip to content

Commit

Permalink
Merge pull request #27 from MiroHibler/export_md
Browse files Browse the repository at this point in the history
Implement MarkDown CheatSheet
  • Loading branch information
pykong authored Feb 25, 2020
2 parents dc70c60 + 24ae0dc commit 40c9760
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### v1.4.0
* Dropped support for Sublime Text 2
* Export to MarkDown via the new command: "Keymaps: Markdown Cheat Sheet"

### v1.3.2

Expand Down
7 changes: 6 additions & 1 deletion Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
"caption": "Keymaps: Cheat Sheet",
"command": "cheat_sheet"
},
{
"id": "markdown_cheat_sheet",
"caption": "Keymaps: Markdown Cheat Sheet",
"command": "markdown_cheat_sheet"
},
{
"id": "find_keymap",
"caption": "Keymaps: Find a keymap for...",
"command": "find_keymap"
}
]
]
5 changes: 5 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"id": "cheat_sheet",
"caption": "Cheat Sheet",
"command": "cheat_sheet"
},
{
"id": "markdown_cheat_sheet",
"caption": "Markdown Cheat Sheet",
"command": "markdown_cheat_sheet"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion messages/1.4.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Install a version below v1.4.0 to continue using on Sublime Text 2.

New features in Keymaps v1.4.0:


- Export to MarkDown via the new command: "Keymaps: Markdown Cheat Sheet"



Expand Down
119 changes: 119 additions & 0 deletions sublime_keymaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ def run(self, edit):
do(renderer, keymap_counter)


class MarkdownCheatSheetCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = Settings(self.view.settings().get("keymaps", {}))
keymap_counter = KeymapScanCounter()
renderer = MarkdownCheatSheetRenderer(settings, self.view.window(), keymap_counter) # noqa: flake8 E501
do(renderer, keymap_counter)


class FindKeymapCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = Settings(self.view.settings().get("keymaps", {}))
Expand Down Expand Up @@ -445,6 +453,117 @@ def extract(self):
}


class MarkdownCheatSheetRenderer:
def __init__(self, settings, window, keymap_counter):
self.settings = settings
self.window = window
self.keymap_counter = keymap_counter

@property
def view_name(self):
"""The name of the new keymaps view. Defined in settings."""
return self.settings["keymaps_title"]

@property
def header(self):
hr = "# Sublime Keymaps CheatSheet\n- {}\n- {}\n- {}\n"
version = "{} @ {}".format(MY_NAME, VERSION)
timestamp = datetime.now() \
.strftime("%A %d %B %Y %H:%M") \
.encode("utf-8") \
.decode("utf-8")
num_keymaps = "{} keymaps found".format(self.keymap_counter)
hr = hr.format(version, timestamp, num_keymaps)
return hr

@property
def view(self):
existing_keymaps = [
v
for v in self.window.views()
if v.name() == self.view_name and v.is_scratch()
]
if existing_keymaps:
v = existing_keymaps[0]
else:
v = self.window.new_file()
v.set_name(self.view_name)
v.set_scratch(True)
v.settings().set("cheat_sheet", True)
return v

def format(self, packages):
key_func = lambda m: m["package"]
packages = sorted(packages, key=key_func)

for package, keymaps in groupby(packages, key=key_func):
keymaps = list(keymaps)
if keymaps:
yield ("header", "{} ({})".format(package, len(keymaps)), {})

for idx, m in enumerate(keymaps, 1):
keys = ""
for key_token in m["keys"]:
keys = keys + " " + " ".join(key_token)
if self.settings["show_pretty_keys"]:
d = PRETTY_KEYS
pattern = re.compile(
r"\b("
+ "|".join(re.escape(key) for key in d.keys())
+ r")\b"
)
keys = pattern.sub(lambda x: d[x.group()], keys)
justif_keys = keys.rjust(LINE_SIZE // 2)
justif_capts = m["caption"].ljust(LINE_SIZE // 2)
line = "| {} | {} |".format(justif_keys, justif_capts)
yield ("keymap", line, m)

def render(self, formatted_keymaps):
"""This blocks the main thread, so make it quick"""
# Header
keymaps_view = self.view
keymaps_view.run_command("select_all")
keymaps_view.run_command("right_delete")
keymaps_view.run_command("append", {"characters": self.header})

# Region : match_dicts
regions = {}

# Keymap sections
for linetype, line, data in formatted_keymaps:
insert_point = keymaps_view.size()
if linetype == "keymap":
insert_space = insert_point
line_ = line.lstrip()
keymaps_view.run_command(
"append",
{"characters": "{}".format(" " * (len(line) - len(line_)))},
)
insert_point = insert_space + (keymaps_view.size() - insert_space)
keymaps_view.run_command("append", {"characters": line_})
rgn = sublime.Region(insert_point, keymaps_view.size())
regions[(rgn.a, rgn.b)] = (rgn, data)
else: # 'header'
hdr_str = "\n\n##{:>45}\n|||\n|-|-|".format(line)
keymaps_view.run_command("append", {"characters": hdr_str})
keymaps_view.run_command("append", {"characters": "\n"})
keymaps_view.add_regions("keymaps", [v[0] for k, v in regions.items()], "")

# Store {Region : data} map in settings
# TODO: Abstract this out to a storage class Storage.get(region) ==> data dict
# Region() cannot be stored in settings, so convert to a primitive type
# d_ = regions
d_ = {"{},{}".format(k[0], k[1]): v[1] for k, v in regions.items()}
keymaps_view.settings().set("keymap_regions", d_)

# Set syntax and settings
keymaps_view.settings().set("line_padding_bottom", 2)
keymaps_view.settings().set("line_padding_top", 2)
keymaps_view.settings().set("word_wrap", False)
keymaps_view.settings().set("command_mode", True)
self.window.focus_view(keymaps_view)


class CheatSheetRenderer:
def __init__(self, settings, window, keymap_counter):
self.settings = settings
Expand Down

0 comments on commit 40c9760

Please sign in to comment.