- HTML tags
- spaces/non-breaking spaces
- regular text, each sentence in a different variable
The part which divides it and stores it in the array is working fine. But I also need to store the type in a variable, which I called "kind". I'm having trouble achieving this.
Please ignore the various MsgBox, they are just there for debugging.
TIA
Code: Select all
; Parse string and split it into 3 categories: html tags, spaces/non-breaking spaces, regular text
#IfWinActive, Notepad++
^F12::
KeyWait, Control
KeyWait, F12
Sleep 100
; Specify the file path
filePath := "D:\\test.txt"
;test.txt has following stored:
;<p> </p><p><span style="font-size:11pt">This is the first sentence. </span></p>Second sentence.</span></p>
; Initialize a variable to store file content
fileContent := ""
inputString := ""
Clipboard := ""
; Read the content of the file into the variable
FileRead, inputString, %filePath%
;inputString := sourceHtml
previousIsText := false ; Initialize the flag for text concatenation
; Initialize array to store elements
elements := {}
kinds := {}
elementIndex := 1
; Regex to split input into parts (tags and text)
pattern := "(\s+|<[^>]+>|[^<\s]+)"
pos := 1
while (pos <= StrLen(inputString)) && RegExMatch(inputString, pattern, match, pos)
{
if (SubStr(match, 1, 1) = "<")
{
; It's an HTML tag
elements["element" . elementIndex] := match
kinds["kind" . elementIndex] := "tag"
previousIsText := false
elementIndex++
}
else
{
; It's text
if (previousIsText)
{
; Concatenate with previous text
elements["element" . (elementIndex - 1)] .= match
}
else
{
elements["element" . elementIndex] := match
previousIsText := true
elementIndex++
}
}
;sleep 300
; this is not working
nowelement := elements["element" . elementIndex]
Send, %nowelement%
Send, {Enter}
Sleep, 100
;this is not working
/*
corrente := elements["element" . elementIndex]
;MsgBox % currentElement
If (RegExMatch(currentElement, "^\s+$") or RegExMatch(currentElement, "^\xA0+$"))
{
kinds["kind" . elementIndex] := "white"
}
else
{
kinds["kind" . elementIndex] := "txt"
}
*/
pos += StrLen(match)
}
; Output the elements
elementCount := elementIndex - 1 ; Get the total number of elements
outputMsg := ""
Loop, % elementCount
{
outputMsg .= elements["element" . A_Index] . "`r"
}
; Sending output to active window
Loop, % elementCount
{
Send, % kinds["kind" . A_Index]
Send, % elements["element" . A_Index]
Send {Enter}
}
Clipboard := outputMsg
sleep 1000
send {Enter}{Enter}{Enter}
; reconstruir target com tags
Loop, % elementCount
{
key := "kind" . A_Index
;currentKind := kinds["kind" . A_Index]
currentKind := kinds[key]
If (RegExMatch(currentKind, "^\s+$") or RegExMatch(currentKind, "^\xA0+$"))
{
Send Whitespace
}
Send, % kinds["kind" . A_Index]
Send, % elements["element" . A_Index]
Send {Enter}
}
Return
[Mod action: Moved topic to the v1 section since this is v1 code. The main section is for v2.]