Sunday, February 7, 2010

Wiki Markup to Rtf

It is actually fairly easy to reformat basic Wiki Markup to to basic rtf with AhK. Here is a script that does this for ConnectedText:

F10 & q:: ;Footnotes to Rtf
clipboard :=
Send ^c
ClipWait
clipboard = %clipboard%

StringReplace, clipboard, clipboard, %A_SPACE%`/`/, %A_SPACE%`{`\i%A_SPACE%, All
StringReplace, clipboard, clipboard, %A_SPACE%`(`/`/, %A_SPACE%`{`\i%A_SPACE%, All
StringReplace, clipboard, clipboard, `/`/%A_SPACE%, `}%A_SPACE%, All
StringReplace, clipboard, clipboard, `/`/`,, `}`,, All
StringReplace, clipboard, clipboard, `/`/`., `}`., All
StringReplace, clipboard, clipboard, `/`/`;, `}`;, All
StringReplace, clipboard, clipboard, `/`/`:, `}`:, All
StringReplace, clipboard, clipboard, `/`/`?, `}`?, All
StringReplace, clipboard, clipboard, `/`/`!, `}`!, All
StringReplace, clipboard, clipboard, `/`/`), `}`), All
StringReplace, clipboard, clipboard, `/`/`], `}`], All
StringReplace, clipboard, clipboard, `/`/`', `}`', All
StringReplace, clipboard, clipboard, `/`/`", `}`", All
StringReplace, clipboard, clipboard, `/`/`n`r, `}`n`r, All

StringReplace, clipboard, clipboard, %A_SPACE%`*`*, %A_SPACE%`{`\b%A_SPACE%, All
StringReplace, clipboard, clipboard, `*`*%A_SPACE%, }%A_SPACE%, All
StringReplace, clipboard, clipboard, `*`*`.,`}`., All
StringReplace, clipboard, clipboard, `*`*`,,`}`,, All
StringReplace, clipboard, clipboard, `*`*`;,`}`;, All
StringReplace, clipboard, clipboard, `*`*`:,`}`:, All
StringReplace, clipboard, clipboard, `*`*`?,`}`?, All
StringReplace, clipboard, clipboard, `*`*`!,`}`!, All
StringReplace, clipboard, clipboard, `*`*`),`}`), All
StringReplace, clipboard, clipboard, `*`*`],`}`], All
StringReplace, clipboard, clipboard, `*`*`',`}`', All
StringReplace, clipboard, clipboard, `*`*`",`}`", All
StringReplace, clipboard, clipboard, `*`*`n`r, `}`n`r, All


StringReplace, clipboard, clipboard, `n`r, `\par, All
StringReplace, clipboard, clipboard, `[`!, \chftn{\footnote \pard\plain \s246 \fs20 {\up6\chftn}, All
StringReplace, clipboard, clipboard, `!`], }, All
Run, Notepad.exe
Winwait, Untitled - Notepad
SendInput ^v
Sleep, 900
Send ^{Home}
SendRaw `{`\rtf1`\ansi`{`\fonttbl`\f0`\fswiss Helvetica;`}`\f0`\pard
Send {Enter}
Send ^{End}
Send {Enter}
SendRaw }
return


Instructions: Select the passage you want to convert; press F10 and Q at the same time. Notepad will open and the conversion will start. Once the conversion finished, save the file with the name of your choice, using the rtf extension. Open it in your word processor.

What it does: It translates italics to italics and bold to bold. It will also translate ConnectedText's footnotes to footnotes in rtf.

I have tested it with Atlantis. It works.

It would, of course, be possible to translate other formatting as well, but I don't need much more, so I have not implemented it. It should also be easy to adapt the script to other Wiki Markup. But I don't know of any personal wiki application that implements footnotes as well as ConnectedText which is one of the many reasons why I use it.

For information on the rtf file format, go to Rich Text Format and scroll to the bottom where the links to the different versions are.

3 comments:

TWatters said...

From Microsoft's website:
"RTF file format
The RTF file format is no longer enhanced to include new features and functionality. Features and functionality that are new to Word 2010 and future versions of Word are lost when they are saved in RTF. In addition, Word 2010 supports a new converter interface based on Open XML Formats."
It looks like exporting to html may be better than exporting to RTF as along term solution to retain formatting.

MK said...

RTF is, however, used by a great variety of different NON-Microsoft applications. It may be an advantage that that it is no longer "enhanced."

I have not upgraded Word since Word 2003 (and do not intend to upgrade it ever again).

That being said, I think HTML has definite advantages.

MK said...

This is simpler. It uses %A_EndChar% (which I was not aware of before):

F10 & q:: ;Footnotes to Rtf
clipboard :=
Send ^c
ClipWait
clipboard = %clipboard%

StringReplace, clipboard, clipboard, %A_SPACE%`/`/, %A_SPACE%`{`\i%A_SPACE%, All
StringReplace, clipboard, clipboard, %A_SPACE%`(`/`/, %A_SPACE%`{`\i%A_SPACE%, All
StringReplace, clipboard, clipboard, `"/`/, `"`{`\i%A_SPACE%, All
StringReplace, clipboard, clipboard, `'/`/, `'`{`\i%A_SPACE%, All
StringReplace, clipboard, clipboard, `/`/%A_SPACE%, `}%A_SPACE%, All
StringReplace, clipboard, clipboard, `/`/`n`r, `}`,, All
StringReplace, clipboard, clipboard, `/`/%A_EndChar%, `}`,, All


StringReplace, clipboard, clipboard, %A_SPACE%`*`*, %A_SPACE%`{`\b%A_SPACE%, All
StringReplace, clipboard, clipboard, `*`*%A_SPACE%, `}%A_SPACE%, All
StringReplace, clipboard, clipboard, `*`*%A_EndChar%, `}`,, All


StringReplace, clipboard, clipboard, `n`r, `\par, All
StringReplace, clipboard, clipboard, `[`!, \chftn{\footnote \pard\plain \s246 \fs20 {\up6\chftn} , All
StringReplace, clipboard, clipboard, `!`], `}, All
Run, Notepad.exe
Winwait, Untitled - Notepad
SendInput ^v
Sleep, 900
Send ^{Home}
SendRaw `{`\rtf1`\ansi`{`\fonttbl`\f0`\fswiss Helvetica;`}`\f0`\pard
Send {Enter}
Send ^{End}
Send {Enter}
SendRaw }
return