Example Script [Not working]: Take CSV and convert to BBcode

Started by Stefan, September 27, 2009, 08:10:13 PM

Previous topic - Next topic

Stefan

Hi all.

To see what is possible by an script
and what is needed from the editor script engine
i post here some test scripts.

Please note that this scripts are NOT written for HippoEDIT right now
and that HippoEDIT didn't even have an scripting support implemented right now.
(script will be converted once HippoEDIT scripting support is implemented)

Hint:
* i am not an programmer,
* that script may not be well designed (mostly i am have less interest if my script works "fair to middling"
    to clean it up, because of i had to do other things is more important mostly)
* test my scripts with test files only
* i give no guarantee that your files are not deleted
   and that your computer will not gonna implode.

* the scripts are free to use and to modify. But please respect the copyright mentions inside the scripts.

(If YOU want to post scripts too, please add an disclaimer as mine too.
You could even use mine for non-working scripts)






Example Script [Not working]: Take CSV and convert to BBcode


This script is to create BBcode tables for an forum.
a) You have to provide the values for the single cells,
      the delimiter between the cells is hard-coded to ','.
b) Then select this CSV text and execute the script.

The script simple search for three things:
1. start of line ==> ^
2. delimiter ====> ,
3. end of line ==> $
And then replace this by BBcode code.

http://en.wikipedia.org/wiki/Comma-separated_values
http://en.wikipedia.org/wiki/BBCode
http://en.wikipedia.org/wiki/Regex



For example:

I have this test text:
1, HippoEDIT, is an editor
2, HippoEDIT, is portable
3, HippoEDIT, use highlighting
4, HippoEDIT, supports RegEx
5, HippoEDIT, use per syntax settings
6, HippoEDIT, have word completion function
7, HippoEDIT, provide column editing
8, HippoEDIT, edit multi files at once


After using the script this is converted to:
(Note: i added '  ----  '  to make the result better visible)

[table]
[tr][td]1 ---- [/td][td] HippoEDIT ---- [/td][td] is an editor[/td][/tr]
[tr][td]2[/td][td] HippoEDIT[/td][td] is portable[/td][/tr]
[tr][td]3[/td][td] HippoEDIT[/td][td] use highlighting[/td][/tr]
[tr][td]4[/td][td] HippoEDIT[/td][td] supports RegEx[/td][/tr]
[tr][td]5[/td][td] HippoEDIT[/td][td] use per syntax settings[/td][/tr]
[tr][td]6[/td][td] HippoEDIT[/td][td] have word completion function[/td][/tr]
[tr][td]7[/td][td] HippoEDIT[/td][td] provide column editing[/td][/tr]
[tr][td]8[/td][td] HippoEDIT[/td][td] edit multi files at once[/td][/tr]
[/table]


Which results to, once posted at an forum:


1 ---- HippoEDIT ---- is an editor
2 HippoEDIT is portable
3 HippoEDIT use highlighting
4 HippoEDIT supports RegEx
5 HippoEDIT use per syntax settings
6 HippoEDIT have word completion function
7 HippoEDIT provide column editing
8 HippoEDIT edit multi files at once





And now, ladies and gentlemen, THE code:


' Save this script as e.g. CSV2BBTable.vbs
' into your PSPad script folder, e.g. "C:\Program Files\PSPad\Script\VBscript\CSV2BBTable.vbs"
' Then select some separated text (see mySEPARATOR) and execute this script from the script menu.
' ------------------------------------------------------------------------------
' author        : Stefan 2007 for PSPad.com and DonationCoder.com
' Based on an proof of concept from "Crono" at http://www.donationcoder.com/Forums/bb/index.php?topic=8504.msg61390#msg61390
' Use code from : Scott Greenberg <me@gogogadgetscott.com> [GoGoModule.vbs]
'                 http://gogogadgetscott.info/computers/cc/snippet.php?sid=34
' Use code from : James Swan  [text_to_list.vbs]
' ------------------------------------------------------------------------------
'ToDo inputbox=BBcode or HTML?
'ToDO inputbox=char to indicate a newline
Const module_name  = "CSV-2-BBTable"
Const module_ver   = "0.001"
Const mySEPARATOR = ","      ' the separator in your CSV file to separate the table cells, e.g. , ; :
Sub Init
    menuName = "&" & module_name
    addMenuItem "Execute &CSV to BBcode Table"     , menuName, "replaceRegExp"
    addMenuItem "&Open this script file"           , menuName, "openFile"
End Sub
Sub openFile
    Set obj1 = newEditor()
    obj1.openFile(moduleFileName(module_name))
End Sub
' ------------------------------------------------------------------------------
'                                 Text editing.
' ------------------------------------------------------------------------------
Dim arrLines
' Pattern Find
Dim vFind(4)
vFind(0)= "#" 'not needed
vFind(1)= mySEPARATOR
vFind(2)= "$" 'end of each line
vFind(3)= "^" 'start of each line

' newtext Replace
Dim vReplace(4)
vReplace(0) = "[br]"
vReplace(1) = "[/td][td]"
vReplace(2) = "[/td][/tr]"
vReplace(3) = "[tr][td]"

Sub replaceRegExp
'*******************************************************************************
' Split function
' author : James Swan    ' Created : 28 September 2005
'*******************************************************************************
Set editor = newEditor()
With editor
.assignActiveEditor()
strInput = .selText()
End With
  If Len(strInput) > 0 Then
  arrLines = Split(strInput, vbCrLf)
      For L = 0 to UBound(arrLines)
          For i = 1 to UBound(vFind)
              arrLines(L) = runRegExpReplace(arrLines(L),vFind(i) ,vReplace(i))
          Next
      Next
      strList = "[table]" & vbCrLf
  For L = 0 To UBound(arrLines)
    strLine = Trim(arrLines(L))
    strList = strList & strLine & vbCrLf
    Next
  strList = strList & "[/table]"
  editor.selText strList
    Else
MsgBox "Please select some text...  ", 0, MODULE_TITLE
End If
Set editor = Nothing
End Sub

' ------------------------------------------------------------------------------
'               Private function: Find and replace with RegEx
' ------------------------------------------------------------------------------
' author    Scott Greenberg <me@gogogadgetscott.com>
Private Function runRegExpReplace(haystack, Pattern, newtext)
    Set regEx = New RegExp
    If Pattern = "" Then
        runRegExpReplace = haystack
        Exit Function
    End If
    regEx.Pattern    = Pattern
    regEx.IgnoreCase = TRUE
    regEx.Global     = TRUE
    runRegExpReplace = regEx.Replace(haystack, newtext)
End Function

Stefan, HippoEDIT beta tester 
HippoEDIT - the editor programmers wants to code thyself when they are dreaming.        -Don't just edit. HippoEDIT!-