Susan's Place Logo

News:

Visit our Discord server  and Wiki

Main Menu

Sanitize_for_SMF.py -- Well, so I’m a geek. *wink*

Started by Kimberly, January 23, 2006, 01:58:51 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Kimberly

Well, so I'm a geek. *wink*

Here is a python script to convert my markup to something these forums can understand.

In short, it will convert

**bold**
*emote*
__underline__
//italics//
and (MS) Word's fancy punctuation marks, "quotes", 'single-quotes', Hy–phen and the handy ellipsis...


In to:

[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:

#! /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:

Add_markup(<pattern>, <replacement string>)


So, if you want to change the color of the emote...
Before:

Add_markup(r'(\*.+?\*)', r'[color=Purple][sup]\1[/sup][/color]')

After:

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.

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>
  •  

rana

Kimberley this thread scares me - I have no idea what you are on about :(

rana
Definately ungeeky through to computer illiterate :(
  •  

Kimberly

*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 (=
  •  

Shelley

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.
  •  

beth

i always thought i was kinda smart till i read this thread.   :icon_help:


beth
  •