Skip to content

Instantly share code, notes, and snippets.

@makiaea
Last active December 14, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makiaea/5099182 to your computer and use it in GitHub Desktop.
Save makiaea/5099182 to your computer and use it in GitHub Desktop.
20131124maki003
# -*- coding: utf-8 -*-
# import modules to get/set text to
# clipboard and open other apps
import clipboard
import webbrowser
#define our method
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
# our text in which the replacement will take place
# we are extracting from the clipboard
my_text = clipboard.get()
reps00 = {' =':':', ';':','}
# this replacement removes a previous or older style of superscripting NB removes </span>
reps01 = {'\n':';', '<span style="vertical-align:super;">':'<sup>', '</span>':'</sup>', '&lt;sup&gt;':'<sup>', '&lt;/sup&gt;':'</sup>'}
# our dictionary with our key:values
# we want to replace '1' with '<sup>1</sup>' etc.
reps02 = {'1':'<sup>1</sup>', '2':'<sup>2</sup>', '3':'<sup>3</sup>', '4':'<sup>4</sup>', '5':'<sup>5</sup>', '6':'<sup>6</sup>', '7':'<sup>7</sup>', '8':'<sup>8</sup>', '9':'<sup>9</sup>', '0':'<sup>0</sup>', '&nbsp;':' '}
# this replacement corrects for doubled superscripting
# trailing/double spaces and *s and /s
# remove unicode 粵
# replace hypen with n-dash
reps03 = {'<sup><sup>':'<sup>', '</sup></sup>':'</sup>', '</sup> ':'</sup>', '</sup>*<sup>':'*', '</sup>/<sup>':'/', ' ':' ', u' 粵':'', '-':u'–'}
# run the function three times, each taking the
# output from the previous step and using
# the next dictionary
txt00 = replace_all(my_text, reps00)
txt01 = replace_all(txt00, reps01)
txt02 = replace_all(txt01, reps02)
txt03 = replace_all(txt02, reps03)
# bind the returned text of the method
# to a variable and send it to the clipboard
clipboard.set(txt03)
# open drafts for manual pasting and further processing (e.g. adding comma and space between cantonese and mandarin transliterations)
webbrowser.open('drafts://')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment