Jump to content

HTML converter


Recommended Posts

Hello everyone. I'm trying to write HTML converter. U write normal text and the programe will convert it into HTML verison. I want it to add "<br>" at the end of each line. Don't know why, the programme first writes normal text without "<br>" and then writes it again, but this time with "<br>" at the end of each line. Here is the code. I hope u managed to understand what I'm talking about.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <File.au3>
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\ \Moje dokumenty\Projekty autoit\Projekty\HTML converter\Form1.kxf
$Form1 = GUICreate("HTML converter", 633, 450, 192, 124)
$Label1 = GUICtrlCreateLabel("Tytuł strony", 24, 16, 60, 17)
$Input1 = GUICtrlCreateInput("Input1", 104, 16, 473, 21)
$Label2 = GUICtrlCreateLabel("Zawartosc", 24, 64, 54, 17)
$Edit1 = GUICtrlCreateEdit("", 104, 64, 473, 300)
GUICtrlSetData(-1, "Edit1")
$Button1 = GUICtrlCreateButton("Generuj", 256, 384, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _FileCreate(@ScriptDir & "\tekst.html")
            FileWriteLine("tekst.html", "<html>") ;const
            FileWriteLine("tekst.html", "<head><title>" & GUICtrlRead($Input1) & "</title></head>")
            FileWriteLine("tekst.html", "<body>");const
            FileWriteLine("tekst.html", GUICtrlRead($Edit1))
            For $x = 3 to _FileCountLines(@ScriptDir & "\tekst.html") Step +1
                FileWriteLine(@ScriptDir & "\tekst.html", FileReadLine(@ScriptDir & "\tekst.html", $x) & "<br>")
            Next

            FileWriteLine("tekst.html", "</body>");const
            FileWriteLine("tekst.html", "</html>") ;const
    EndSwitch
WEnd
Link to comment
Share on other sites

$workingString = StringReplace($workingString, @CRLF, "<br>" & @LF) ?

You are probably better off to finish writing the file first, then FileRead() it and do the replace as above. Then open it in Write mode (2) then re-write the file and FileClose() it.

$sStr - FileRead("Some.htm")
$sStr = StringReplace($sStr, @CRLF, "<br>" & @LF);; $sStr = StringReplace($sStr, @CR, "<br>") should also be valid.
$hFile = FileOpen("some.htm", 2)
If $hFile <> -1 Then
    FileWrite($hFile, $sStr)
    FileClose($hFile)
EndIf

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

This is slightly different to GEOSoft's response. Perhaps somebody else can improve on it.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <File.au3>

$Form1 = GUICreate("HTML converter", 633, 450, 192, 124)
$Label1 = GUICtrlCreateLabel("Tytul strony", 24, 16, 60, 17)
$Input1 = GUICtrlCreateInput("Input1", 104, 16, 473, 21)
$Label2 = GUICtrlCreateLabel("Zawartosc", 24, 64, 54, 17)
$Edit1 = GUICtrlCreateEdit("", 104, 64, 473, 300)
GUICtrlSetData(-1, "Edit1")
$Button1 = GUICtrlCreateButton("Generuj", 256, 384, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _FileCreate(@ScriptDir & "\tekst.html")
            FileWriteLine("tekst.html", "<html>") ;const
            FileWriteLine("tekst.html", "<head><title>" & GUICtrlRead($Input1) & "</title></head>")
            FileWriteLine("tekst.html", "<body>");const

            $data = GUICtrlRead($Edit1) ; Grab the data
            $data = StringReplace($data, @CR, "<br>" & @CR) ; Add <br> to each line and =>
            FileWriteLine("tekst.html", $data) ; Dump the data in one hit

            FileWriteLine("tekst.html", "</body>");const
            FileWriteLine("tekst.html", "</html>") ;const
    EndSwitch
WEnd

I made as few changes to your code as possible. If you want the page to finish with <br>, just concatonate it to the end of $data before dumping. I hope you find this helpful.

Edit: The reason the html repeats (in your code) is because you first write it to the html document. Then you copy everything from (and including) the third line. Then you add it again with <br> tags. I don't see a reason to write to the document until you have formated the html.

Edited by czardas
Link to comment
Share on other sites

Thank you. That's just what I've needed.

zwierzak: You will only be able to generate a limiited amount of html using this approach. I can see it being useful for someone who has no knowledge of html at all, if they just intend to update some simple text on a page. Perhaps you could incorporate <div> or <p> tags, but it will be difficult to do much more. I suggest that you first decide which html tags you intend to use before you write any more code. Then try to think how you might do that. Best of luck with it.
Link to comment
Share on other sites

I'm not really good at html. Never learned it for a longer time. I just want to write this prorgam to get bigger skill in autoit. I know the basics of html and IMO this is enough for my program. For instance, I have never heard about <div> or <p> :idea: Maybe one day I will think of improving this program so it could be used by web page - writers.

Link to comment
Share on other sites

Then I suggest you bookmark this page

http://www.w3schools.com/html/default.asp

Until you know at least enough to set up a decent web page, anything you attempt to automate the process is doomed to failure.

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

I think your program is a good idea. You learn a lot by trying things out. Sometimes things don't work immediately and then you have to solve it. I think that is true even for professional programmers. That's why they have bugs. :idea:

Edit: That's a really good website that GEOSoft recommended to you.

Edited by czardas
Link to comment
Share on other sites

I think your program is a good idea. You learn a lot by trying things out. Sometimes things don't work immediately and then you have to solve it. I think that is true even for professional programmers. That's why they have bugs. :idea:

Edit: That's a really good website that GEOSoft recommended to you.

I remember the days when I spent a lot of time on the various W3 Schools pages and I found them excellent.

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