General Discussions => General discussions => Topic started by: Kimberly on January 23, 2006, 01:58:51 AM Return to Full Version
Title: Sanitize_for_SMF.py -- Well, so I’m a geek. *wink*
Post by: Kimberly on January 23, 2006, 01:58:51 AM
Post by: Kimberly on January 23, 2006, 01:58:51 AM
Well, so I'm a geek. *wink*
Here is a python (http://www.python.org) script to convert my markup to something these forums can understand.
In short, it will convert
In to:
Which is:
All in all it is pretty simple, fill a list with (pattern,replace) tuples; Get the text from the clipboard then for each item in the list substitute matches of pattern with the replace string. Once that is done, stuff the string back into the clipboard.
So, usage is dead simple:
Tis as easy as that.
The script:
Note that python scripts are sensitive to indent.
Adding/changing the markup is quite simple and accomplished via the Add_markup function:
So, if you want to change the color of the emote...
Before:
After:
Note that all that was changed was the "Purple" text to "Navy" I.e. different color.
There is NO obligation to use this of course, it's just one of my personal little scripts I thought someone might find handy.
I am happy to answer any questions regarding.
P.s. I have the same functionality in vim too if anyone would rather have that.
Here is a python (http://www.python.org) script to convert my markup to something these forums can understand.
In short, it will convert
Code Select
**bold**
*emote*
__underline__
//italics//
and (MS) Word's fancy punctuation marks, "quotes", 'single-quotes', Hy–phen and the handy ellipsis...
In to:
Code Select
[b]bold[/b]
[color=Purple][sup]*emote*[/sup][/color]
[u]underline[/u]
[i]italics[/i]
and (MS) Word's fancy punctuation marks, "quotes", 'single-quotes', Hy-phen and the handy ellipsis...
Which is:
Quote
bold
*emote*
underline
italics
and (MS) Word's fancy punctuation marks, "quotes", 'single-quotes', Hy-phen and the handy ellipsis...
All in all it is pretty simple, fill a list with (pattern,replace) tuples; Get the text from the clipboard then for each item in the list substitute matches of pattern with the replace string. Once that is done, stuff the string back into the clipboard.
So, usage is dead simple:
- 1. Write your text in word or where ever you like
- 2. Copy your post into the clipboard
- 3. Run this script
- 4. Paste your (now sanitized) post into the edit box in your browser.
Tis as easy as that.
The script:
Code Select
#! /usr/bin/env python
#{{{ File header information
# vim:ff=unix ts=4 ss=4
# \file Sanitize_for_SMF.py
# \date Sun, 22 Jan 2006 23:30 PST
#
# \brief take the contents of the clipboard, process it, then paste the results to the clipboard.
#
# \author Kimberly Kelly <sreny@srenyqernzf.pbz> (Rot13ed)
# \note Public Domain, enjoy!
# Version: 0.1
#}}}
###################################################
# No //need// to worry about Tokens nor Add_markup
###################################################
Tokens = [] # Initialize Tokens to an empty list
def Add_markup(pat, rep): # To make adding/changing markup a bit more clear.
Tokens.append( (pat, rep) )
###################################################
# OPTION VARS:
###################################################
#
## MS Word's fancy text to the not so fancy equivalent
#
Add_markup(r'[\x93\x94]', r'"')
Add_markup(r'[\x91\x92]', r"'")
Add_markup(r'\x96', r'--')
Add_markup(r'\x85', r'...')
#
## My (wiki) markup to something pretty for SMF (forums)
#
# **bold**
Add_markup(r'\*\*(.+?)\*\*', r'[b]\1[/b]')
# *emote* (must be after **bold**)
Add_markup(r'(\*.+?\*)', r'[color=Navy][sup]\1[/sup][/color]')
# __underline__
Add_markup(r'__(.+?)__', r'[u]\1[/u]')
# //italics//
Add_markup(r'\/\/(.+?)\/\/', r'[i]\1[/i]')
###################################################
###################################################
# No //need// to worry about what is below
###################################################
import re
import win32clipboard
import win32con
def main():
"Entry point into this script."
work = GetClipboardText()
for entry in Tokens:
pat = entry[0]
rep = entry[1]
work = re.sub(pat, rep, work)
PasteText(work)
def GetClipboardText():
"Remember to EmptyClipboard though!"
win32clipboard.OpenClipboard()
try:
got = win32clipboard.GetClipboardData(win32con.CF_TEXT)
finally:
win32clipboard.CloseClipboard()
return got
# See also: C:\Python22\Lib\site-packages\win32\Demos\win32clipboardDemo.py
def PasteText(p_text):
"from win32clipboardDemo.py"
"Remember to EmptyClipboard though!"
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
try:
text = p_text
win32clipboard.SetClipboardText(text)
finally:
win32clipboard.CloseClipboard()
if __name__ == '__main__':
"Entry point when ran just by itself."
main()
#
# EOF
Note that python scripts are sensitive to indent.
Adding/changing the markup is quite simple and accomplished via the Add_markup function:
Code Select
Add_markup(<pattern>, <replacement string>)
So, if you want to change the color of the emote...
Before:
Code Select
Add_markup(r'(\*.+?\*)', r'[color=Purple][sup]\1[/sup][/color]')
After:
Code Select
Add_markup(r'(\*.+?\*)', r'[color=Navy][sup]\1[/sup][/color]')
Note that all that was changed was the "Purple" text to "Navy" I.e. different color.
There is NO obligation to use this of course, it's just one of my personal little scripts I thought someone might find handy.
I am happy to answer any questions regarding.
P.s. I have the same functionality in vim too if anyone would rather have that.
Code Select
amenu Fe&ral's.Sanitize\ &Words\ text <esc><esc>:%s/[""]/"/ge\|:%s/['']/'/ge\|:%s/–/--/ge\|:%s/.../.../ge<cr>
amenu Fe&ral's.Prep\ For\ &SMF <esc><esc>:%s/\*\*\(.\{-1,}\)\*\*/[b]\1[\/b]/ge\|:%s/\*\(.\{-1,}\)\*/[color=Purple][sup]\0[\/sup][\/color]/ge\|:%s/__\(.\{-1,}\)__/[u]\1[\/u]/ge\|:%s/\/\/\(.\{-1,}\)\/\//[i]\1[\/i]/ge<cr>
amenu Fe&ral's.Prep\ For\ &HTML <esc><esc>:%s/\*\*\(.\{-1,}\)\*\*/<b>\1<\/b>/ge\|:%s/\*\(.\{-1,}\)\*/<em>\0<\/em>/ge\|:%s/__\(.\{-1,}\)__/<u>\1<\/u>/ge\|:%s/\/\/\(.\{-1,}\)\/\//<i>\1<\/i>/ge<cr>
Title: Re: Sanitize_for_SMF.py -- Well, so I’m a geek. *wink*
Post by: rana on January 23, 2006, 05:11:50 AM
Post by: rana on January 23, 2006, 05:11:50 AM
Kimberley this thread scares me - I have no idea what you are on about :(
rana
Definately ungeeky through to computer illiterate :(
rana
Definately ungeeky through to computer illiterate :(
Title: Re: Sanitize_for_SMF.py -- Well, so I’m a geek. *wink*
Post by: Kimberly on January 23, 2006, 01:21:50 PM
Post by: Kimberly on January 23, 2006, 01:21:50 PM
*smile*
To be honest it is just a fast and convenient way to convert **this** kind of text //which// I find easier to write into the format that these forums use so that it comes out pretty, or at least readable anyway *grin*
Becomes:
*smile*
To be honest it is just a fast and convenient way to convert this kind of text which I find easier to write into the format that these forums use so that it comes out pretty, or at least readable anyway *grin*
It does nothing more (=
To be honest it is just a fast and convenient way to convert **this** kind of text //which// I find easier to write into the format that these forums use so that it comes out pretty, or at least readable anyway *grin*
Becomes:
*smile*
To be honest it is just a fast and convenient way to convert this kind of text which I find easier to write into the format that these forums use so that it comes out pretty, or at least readable anyway *grin*
It does nothing more (=
Title: Re: Sanitize_for_SMF.py -- Well, so I’m a geek. *wink*
Post by: Shelley on January 23, 2006, 01:36:29 PM
Post by: Shelley on January 23, 2006, 01:36:29 PM
QuoteTo be honest it is just a fast and convenient way to convert this kind of text which I find easier to write into the format that these forums use so that it comes out pretty, or at least readable anyway *grin*
I have to say I'm with you Rana. Fast and easier not for this little black duck.
Title: Re: Sanitize_for_SMF.py -- Well, so I’m a geek. *wink*
Post by: beth on January 23, 2006, 01:38:05 PM
Post by: beth on January 23, 2006, 01:38:05 PM
i always thought i was kinda smart till i read this thread. :icon_help:
beth
beth