Jump to content

[Resolved, finished script near bottom.] TXT to HTML code?


Recommended Posts

Good Morning!

Say I have a text file with these lines that I need to convert to an html format:

The Charge Of The Light Brigade
by Alfred, Lord Tennyson

Memorializing Events in the Battle of Balaclava, October 25, 1854

Half a league, half a league,
Half a league onward,
All in the valley of Death
Rode the six hundred.

'Forward, the Light Brigade!
Charge for the guns!' he said:
Into the valley of Death
Rode the six hundred.

'Forward, the Light Brigade!'
Was there a man dismay'd ?
Not tho' the soldier knew
Some one had blunder'd:

Their's not to make reply,
Their's not to reason why,
Their's but to do and die:
Into the valley of Death
Rode the six hundred.

If no html coding is put in, it'll read as something like this mess with no carriage returns in the html output:

The Charge Of The Light Brigade by Alfred, Lord Tennyson Memorializing Events in the Battle of Balaclava, October 25, 1854 Half a league, half a league,...

It would need to have all carriage returns replaced with <br> for html line breaks so that I get something like this below which would restore the line breaks and would display in the html output the same as in the text file format example above:

The Charge Of The Light Brigade<br>by Alfred, Lord Tennyson<br><br>Memorializing Events in the Battle of Balaclava, October 25, 1854<br><br>Half a league, half a league,...

.............................................................

I'm using this means to create the html file:

#include <File.au3>
    $PATH = InputBox("Please enter path to the text file.","Path",""," ","450","125","700","150")
    $TxtFile = $PATH
    ClipPut(FileRead($TxtFile))oÝ÷ Ù«­¢+Ø$ÀÌØíQµÁ%1ôͭѽÁ¥ÈµÀìÅÕ½ÐìÀäÈí!Q51¥±´QµÁ½ÉÉ乡ѵ°¸¸¸¡Ñ¸¤ÅÕ½Ðì(%¥±±Ñ ÀÌØíQµÁ%1¤(%¥±]É¥Ñ ÀÌØíQµÁ%1°ÀÌØíQaQÕµÀ¤

Is there a way to replace the text carriage returns with <br> text so that correct format is sent to the clipboard?

Thanks.

Edited by Diana (Cda)
Link to comment
Share on other sites

I think this would be a lot easier

_FileReadToArray($Path,$aArray)
For $i = 1 To $aArray[0]
StringReplace($aArray[$i],@CRLF,"<br>")
Next

Note: You may need to use @CR or @LF instead. If you want it to work for 100% then use

_FileReadToArray($Path,$aArray)
For $i = 1 To $aArray[0]
StringReplace($aArray[$i],@CRLF,"<br>")
StringReplace($aArray[$i],@CR,"<br>")
StringReplace($aArray[$i],@LF,"<br>")
Next

but that may add extra linebreaks.

Edited by dbzfanatic
Link to comment
Share on other sites

This simple function seems to work.

I copied and pasted the "The Charge Of The Light Brigade" text into NotePad, and saved as Charge.txt file in the same directory as the script.

#include <File.au3>

FileTxtToHtml(@ScriptDir & "\Charge.txt", @ScriptDir & "\ChargeHtml.html")

ShellExecute(@ScriptDir & "\ChargeHtml.html")

Func FileTxtToHtml($sFileIn, $FileOut)  
    Local $aArray
    _FileReadToArray($sFileIn, $aArray)
    Local $hFileout = FileOpen($FileOut,10)
    FileWrite($hFileout,"<html>" & @CRLF & "<head>" & @CRLF & _
            @CRLF & "</head>" & @CRLF & "<body>" & @CRLF & "<p>")       
    For $i = 1 To $aArray[0]
        $aArray[$i] &= "<br>"
        FileWrite($hFileout,$aArray[$i])
    Next
    FileWrite($hFileout,"</p>" & @CRLF & "</body>" & @CRLF & "</html>")
    FileClose($hFileout)
EndFunc   ;==>Txt2Html
Link to comment
Share on other sites

$sStr = FileRead(@ScriptDir & "\Charge.txt")

$sStr = StringStripCR($sStr)

ClipPut (StringReplace($sStr, @LF, "<br>"))

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

$sStr = FileRead(@ScriptDir & "\Charge.txt")

$sStr = StringStripCR($sStr)

ClipPut (StringReplace($sStr, @LF, "<br>"))

Wow, GEOsoft, as always, you've provided a format I can understand! My hats off! <g> Thanks, thanks, thanks!

That was perfect so far <crossing fingers>. I tend to format the web text snippets in the same manner so if it worked on all the trials so far, should do a good job consistently. The script is s ready to go though I will probably come back to fine-tune it along the way. I've used a simple inputbox to save space and my sanity because GUIs still take me some time to do up! <g> But at some point, I'll make a looping GUI so that I can produce as many printouts in tandem as I need along with some added functionality as "stay on top", etc.

Here's how the script stands now and I think it's good to go.:

;
; AutoIt 3x
;
#include <_PartitionLetters.au3>     ; this replaces drive letters
#include<_WEReplacement.au3>     ; references a Windows Explorer replacement program
#NoTrayIcon     ; AutoIt's icon doesn't show in systray
TraySetIcon("Shell32.dll", 153)     ; changes the icon displayed in the systray
AutoItSetOption("WinTitleMatchMode", 2)     ; this allows partial window titles to be valid!


;===============================================================
$WebpageTitlebarName = "Research1"
$WebpageTitle        = "Research Printout"
;===============================================================


;-----------------------------------------------------------------------------------------------------------------------------------------------
;  1.  HTML page contents from a text file:
    #include <File.au3>
    $PATH = InputBox("Please enter path to the text file.","Path",""," ","450","125","700","150")
    $TxtFile = $PATH
;   ClipPut(FileRead($TxtFile))
    $sStr = FileRead($TxtFile)
    $sStr = StringStripCR($sStr)
    ClipPut (StringReplace($sStr, @LF, "<br>"))
;-----------------------------------------------------------------------------------------------------------------------------------------------
;  2.  Create HTML file on desktop and send information to it:
    $TempFILE = @DesktopDir & "\HTMLfile- Temporary.html"
    FileDelete($TempFILE)
    $TEXTdump = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">' & @CRLF & '<HTML><HEAD><TITLE>' & $WebpageTitlebarName & '</TITLE>' & @CRLF & '<META content="text/html; charset=windows-1252" http-equiv=Content-Type>' & @CRLF & '<META content="MSHTML 5.00.2614.3500" name=GENERATOR></HEAD>' & @CRLF & '<BODY aLink=#ff9933 bgColor=#C8B89B link=#003399 text=#000000 vLink=#996633>' & @CRLF & @CRLF & '<font color="#804040" face="times new roman"></font>' & @CRLF & @CRLF & '<a name="linkTop"><center><h1><b><u>' & $WebpageTitle & '</u></b></h1></center><br>' & @CRLF & @CRLF & '<h4>' & ClipGet() & '</h4>' & @CRLF & '<center><img src="' & @CRLF & '" alt=""></center><br>' & @CRLF & '</CENTER></BODY></HTML>'
    FileWrite($TempFILE, $TEXTdump)
;-----------------------------------------------------------------------------------------------------------------------------------------------
;  3.  Launch HTML file in browser and in text file to add picture pathname:
    If FileExists($TempFILE) Then ShellExecute($TempFILE, "", "", "Open", @SW_MAXIMIZE)
        WinWait($TempFILE)
    If FileExists($TempFILE) Then ShellExecute($LaunchMetapadHOME, $TempFILE, "", "Open", @SW_MAXIMIZE)
        WinWait("metapad")
    MsgBox(262160,"Instructions.",'Add the path to the image file after ''<img src="''.')
(Pay no mind to some of my idiosyncratic syntax like my personal UDFs. Those things are there to just make my life easier.)

Cheers. <g>

Link to comment
Share on other sites

This simple function seems to work.

I copied and pasted the "The Charge Of The Light Brigade" text into NotePad, and saved as Charge.txt file in the same directory as the script.

#include <File.au3>

FileTxtToHtml(@ScriptDir & "\Charge.txt", @ScriptDir & "\ChargeHtml.html")

ShellExecute(@ScriptDir & "\ChargeHtml.html")

Func FileTxtToHtml($sFileIn, $FileOut)  
    Local $aArray
    _FileReadToArray($sFileIn, $aArray)
    Local $hFileout = FileOpen($FileOut,10)
    FileWrite($hFileout,"<html>" & @CRLF & "<head>" & @CRLF & _
            @CRLF & "</head>" & @CRLF & "<body>" & @CRLF & "<p>")       
    For $i = 1 To $aArray[0]
        $aArray[$i] &= "<br>"
        FileWrite($hFileout,$aArray[$i])
    Next
    FileWrite($hFileout,"</p>" & @CRLF & "</body>" & @CRLF & "</html>")
    FileClose($hFileout)
EndFunc   ;==>Txt2Html
Thank you. I'll also give this a try. <g>
Link to comment
Share on other sites

Just remembered something about html. Best you make the following change.

$sStr = FileRead(@ScriptDir & "\Charge.txt")
$sStr = StringStripCR($sStr)
$sStr = StringReplace(@LF, "<br>")
ClipPut (StringReplace($sStr, "<br><br>", "<br>&nbsp;<br>"))

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...