Contents

Use This One Trick to Find SuperMemo's Element Location

Abstract
TL;DR: Without the HTML component (question or answer component) being in focus, press ctrl+c to copy the meta-data of the current Element.
Info
This article assumes you have a bit of coding experience in order to manipulate the HTML programmatically.

Introduction

One day I found that with ctrl+c, I could copy the current Element’s meta-data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
Begin Element #56
Source=s:\supermemo\systems\zenmode
Parent=4
ParentTitle=Misc
Priority=72.97297
Begin ElementInfo #56
Title=Linguistic the free encyclopedia
Type=Topic
Status=Dismissed
FirstGrade=8
...
DisplayAt=255
Hyperlink=0
HTMName=Title: Linguistic the free encyclopedia Linguistics From Wikipedia, the free encyclopedia ...
**HTMFile=s:\supermemo\systems\zenmode\elements\4\50.HTM**
TestElement=0
ReadOnly=0
FullHTML=1
Style=0
End Component #1

The most important field of our interest is HTMFile=s:\supermemo\systems\zenmode\elements\4\50.HTM. This tells us where the HTML is stored on the disk. Since each Element is just an HTML, you can use different tools to manipulate the HTML content after identifying its file’s location.

How I Use It

My workflow mainly consists of two parts: first use an AHK script to get a branch’s Elements, then feed the list to Python for further processing. For example, I don’t like the added blue colon after splitting a Topic (see gif below).

I use the following AHK script to get all the Elements' locations, then I copy the string to the python script for find and replace execution.

1. AHK script: F1_Get_Element_Locations.ahk

What this script does:

  1. Prompting for the number of times to go down the Knowledge Tree (KT)

  2. Traversing the top Elements in KT to copy its HTML location

  3. Save the raw string to Clipboard

To use it, with the starting Element in focus, press F1 (or any trigger):

./images/DemoGif.gif

The following is the end result: a comma-separated Python raw string:

1
2
3
4
5
6
element_locations = """
r's:\supermemo\systems\zenmode\elements\1\24\543.HTM',
r's:\supermemo\systems\zenmode\elements\1\24\548.HTM',
r's:\supermemo\systems\zenmode\elements\1\23\539.HTM',
r's:\supermemo\systems\zenmode\elements\1\22\528.HTM'
"""

2. Python script: batch find and replace with regex

traverse_html_regex_replace_and_replace.py

Explanation

1
2
3
BACKUP_LOCATION = (
    ""  # string of the directory of where you want to store the backup HTML
)

For example, “C:\Users\user\Downloads\SuperMemo_Elements_Backup\”

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Smart single quote
smart_single_quote = (
    re.compile(r"&#821[67];", re.U | re.I),
    r"'",
)

# Smart double quote
smart_double_quote = (
    re.compile(r"&#822[01];", re.U | re.I),
    r'"',
)

# remove the SuperMemo-generated blue colon that appears after splitting
remove_blue_colon_after_splitting = (
    re.compile(r'<strong><font color="blue">\s*:\s*</font></strong>', re.U | re.I),
    r"",
)

These are regex. For more about regex, please see Regular Expressions: Regexes in Python

1
2
3
4
5
6
file_list = [
    r"s:\supermemo\systems\zenmode\elements\2\27.HTM"
    r"s:\supermemo\systems\zenmode\elements\2\28.HTM"
    r"s:\supermemo\systems\zenmode\elements\2\29.HTM"
    r"s:\supermemo\systems\zenmode\elements\2\30.HTM"
]

This is the list containing all the Elements' locations. Ideally it should be the result from the AHK script above.

1
2
3
4
5
find_replace_list = [
    smart_single_quote,
    smart_double_quote,
    # remove_blue_colon_after_splitting
]

Uncomment (remove the initial #) so that the script will also run the regex for removing the blue colon that are auto-generated from splitting a Topic.

For more info about what this script does, please read the docstring in the file.

PS: I’m aware that SuperMemo has built-in functions for find and replace, but it doesn’t support regex. For more sophiscated text manipulation, regex is a better tool.

PS II: I’m also aware that for proper HTML manipulation, one should use BeautifulSoup, but this script works good enough for me for now.

Conclusion

The takeaway is really about pressing ctrl+c to copy the meta-data of the current Element.

This is just my particular workflow for handling and modifying the HTML. After getting the HTML locations, you can use any tools to manipulate the HTML: shell script, Python, JavaScript, etc. I use Python for its clean syntax and quick prototyping.