Using PureBasic to create websites?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Using PureBasic to create websites?

Post by Lunasole »

wysardry wrote:Although I prefer BASIC syntax, Perl is well known for its text processing capabilities, which is why I'm still slightly undecided.
Then just spend some time and check if PB provides similar capabilities, nothing to do. Or enum them here and someone will answer
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Using PureBasic to create websites?

Post by wysardry »

Okay, one of the really handy features of Perl is being able to define strings that contain multiple lines of text. I've seen examples in the PureBasic manual that show multiple strings being assigned to one string to do this, but no examples of multiple line strings.

IIRC, in Perl, something like this would work:-

Code: Select all

$my_page = qq~<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<p>Welcome to My Page</p>
</body>
</html>~;
print $my_page;
Everything between the first and second ~ (or whatever character you choose) is treated as one string including newline characters. I also vaguely remember being able to include variables in such strings.

Can PureBasic do this? If so, where is it mentioned in the online manual?
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Using PureBasic to create websites?

Post by Demivec »

wysardry wrote:Can PureBasic do this? If so, where is it mentioned in the online manual?
Though not an exact replica, here are three variations:

Code: Select all

;escaped string, single line format, newlines signaled with escape sequence
my_page$ = ~"<!DOCTYPE html>\n<html>\n<head>\n<title>My Page</title>\n</head>\n<body>\n<p>Welcome To My Page</p>\n</body>\n</html>"
Debug my_page$
Debug "-----------"

;escaped string, mult-line continuation format, newlines signaled with escape sequence
my_page2$= ~"<!DOCTYPE html>\n" + 
           ~"<html>\n" + 
           ~"<head>\n" + 
           ~"<title>My Page</title>\n" + 
           ~"</head>\n" + 
           ~"<body>\n" + 
           ~"<p>Welcome To My Page</p>\n" + 
           ~"</body>\n" + 
           ~"</html>"

Debug my_page2$
Debug "-----------"

;normal string, multi-line continuation with #LF$ constant for newlines
my_page3$= "<!DOCTYPE html>" + #LF$ +
           "<html>" + #LF$ +
           "<head>" + #LF$ +
           "<title>My Page</title>" + #LF$ +
           "</head>" + #LF$ +
           "<body>" + #LF$ +
           ~"<p>Welcome To My Page</p>" + #LF$ +
           "</body>" + #LF$ +
           "</html>"

Debug my_page3$
Manual reference: General Syntax rules
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Using PureBasic to create websites?

Post by wysardry »

That's the page I saw the examples on. I was hoping there was a less cumbersome method.

It would be even more complicated in actual use, as HTML code can include quotation marks which would need to be escaped in PureBasic (but not in my Perl example as the tilde is the string delimiter there).

I'm guessing that reading strings from a text file line by line would be the way to go, although I'm not sure if you can embed other variables in there too. Say, like this:-

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
<title>$my_title</title>
</head>
<body>
<h1>$my_title</h1>
$my_page_content
</body>
</html>
If PureBasic can't replace strings inside files like that, then I would imagine that the next best thing would be to have it search for and replace certain codes, something like this:-

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
<title><%MY_TITLE%></title>
</head>
<body>
<h1><%MY_TITLE%></h1>
<%MY_PAGE_CONTENT%>
</body>
</html>
I made the replaceable code text uppercase so that it stands out amongst the lowercase HTML tags, but lowercase would also work.

ASP tags look similar, so most HTML editors shouldn't have a problem with them in that format. An alternative would be to use comment tags, but the program would then have to be told to ignore actual comments.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Using PureBasic to create websites?

Post by Marc56us »

You can use placeholders in text and then use ReplaceString()
(like s/// in Perl)

There are many functions to manipulate Strings
http://www.purebasic.com/documentation/ ... index.html

ReplaceString() eq s///
http://www.purebasic.com/documentation/ ... tring.html
String$ = ReplaceString(String$, StringToFind$, ReplacementString$ [, Mode [, StartPosition [, NbOccurrences]]])

And best of all RegularExpression (not easy to use in PB like in Perl, but work fine and faster than String)
http://www.purebasic.com/documentation/ ... index.html
... reading strings from a text file line by line would be the way to go ...
Not the only way.
You can read all bytes in a text file in one time, and whatever content (Ascii, Unicode, UTF-8)
and put it in a string
http://www.purebasic.com/documentation/ ... ddata.html

PS. HTML does not care about CR/LF except in <PRE></PRE>

:wink:
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Using PureBasic to create websites?

Post by kenmo »

Some comments...

1. Perl has more advanced string processing built in, but PureBasic is absolutely up to this task. The code just might be more verbose.

2. You can hard-code your HTML strings into the PB code to get it up and running, but I would 100% advise you move it into external files. It would eliminate the issues of escaping quotes and linebreaks, it would be more versatile, and you wouldn't need to open/rebuild your PB program for every HTML change. You could edit these HTML templates in any editor like a regular webpage file.

3. You can read entire text files at once, instead of line by line, with ReadString(File, #PB_File_IgnoreEOL). You don't need to do ReadData() --> byte buffer --> string conversion.

4. PB doesn't automatically replace variables in strings like "$myTitle" but it's simple enough to use ReplaceString()

5. What you do need to remember is to escape characters like & < > in your text when filling in the HTML, such as & --> & . PB doesn't have built-in functions for this, but I'm sure there's plenty of example code around here.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Using PureBasic to create websites?

Post by skywalk »

Marc56us wrote:ReplaceString() eq s///
http://www.purebasic.com/documentation/ ... tring.html
String$ = ReplaceString(String$, StringToFind$, ReplacementString$ [, Mode [, StartPosition [, NbOccurrences]]])

And best of all RegularExpression (not easy to use in PB like in Perl, but work fine and faster than String)
http://www.purebasic.com/documentation/ ... index.html
To clarify, the RegularExpression lib is many many times slower than PB code. :!: Use of RegularExpression is to avoid writing custom parsing code at the expense of speed.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Using PureBasic to create websites?

Post by wysardry »

Thanks guys. I'll have to experiment a little to see which commands work faster, but it's starting to look like a combination of external template files and string replacement is the way to go.

I may try a few of the XML functions too.

I'll likely avoid ReadData() as it needs an area of memory specified for storage and a length for the data to be read, which I probably won't know.

Although browsers don't care about newline characters in HTML, it would be a lot easier for me to find any errors in the created pages if they were there.
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Using PureBasic to create websites?

Post by wysardry »

I've reached chapter 71 of the PDF version of the manual, so far. There are quite a few commands and functions that look like they would be useful. :)

The more I think about this, the more likely it seems that the program could be useful to others. With that in mind, is there an obvious way to include a configuration file for the compiled program, that could be edited by users and have it contain comments (to explain what each setting is for)?
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Using PureBasic to create websites?

Post by Marc56us »

Hi wysardry,
wysardry wrote:I've reached chapter 71 of the PDF version of the manual, so far. There are quite a few commands and functions that look like they would be useful. :)
There are not many new users who read all the documentation from the beginning. Congratulations!
wysardry wrote:The more I think about this, the more likely it seems that the program could be useful to others. With that in mind, is there an obvious way to include a configuration file for the compiled program, that could be edited by users and have it contain comments (to explain what each setting is for)?
The classic and easy way: INI file with Preference
http://www.purebasic.com/documentation/ ... index.html

Another way JSON files
http://www.purebasic.com/documentation/json/index.html
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Using PureBasic to create websites?

Post by #NULL »

there is a tool named jaPBdoc wich generates html documentation from purebasic source code comments:
http://www.purebasic.fr/english/viewtop ... 14&t=22605
http://www.purebasic.fr/english/viewtop ... 48#p487448 (latest version i know of)
it might be interesting for you to look at the code.
to get you started:
- there is a html template 'PBDoc.html'
- it gets included in the executable (in jaPBDoc.pb)

Code: Select all

DataSection
pbdoctemplate:
IncludeBinary "PBDoc.html"
endpbdoctemplate:
EndDataSection
- it will be read into a string

Code: Select all

TemplateString = PeekS(?pbdoctemplate, ?endpbdoctemplate - ?pbdoctemplate, #PB_UTF8)
- the content parts are built up using small hardcoded templates:

Code: Select all

Template = "<TR BGCOLOR="+#DQUOTE$+"white"+#DQUOTE$+"><TD><DL><DT>"
Template + "<code><b>%NAME%</b></code></DT><DD>%DESC%</DD><DL></TD></TR>"
...
Temp = ReplaceString(Template, "%NAME%", constant()\Name)
Temp = ReplaceString(Temp, "%DESC%", constant()\Desc)
- and are then replaced into the final output

Code: Select all

Temp = GetConstantsOverview()
Template = ReplaceString(Template, "%CONSTANTSDESCRIPTION%", Temp)
lexer.pbi (handles the parsing of the PB sourcecode that is to be documented) can be used for testing:

Code: Select all

./jaPBDoc lexer.pbi
(or jaPBDoc.exe in Windows)
generates 'lexer.pbi.html'
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Using PureBasic to create websites?

Post by wysardry »

Marc56us: Thanks. It looks like the preferences option would be the easiest to understand (for me and other users). Being able to write comments in the file from within the program is a handy feature.

I am starting to skip parts of the manual now, if they don't seem relevant or are too advanced, but I am at least reading what each command or function does, so I know what options there are. I will probably skip the 3D engine commands when I reach them, as I can't see me using them any time soon.

#NULL: Thanks. I'll take a closer look at the code once I've read more of the manual. It will be easier for me to understand what the program is doing that way. I do have experience of other versions of BASIC, but PureBasic does some things differently.

I think flat file databases will be heavily involved.
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Using PureBasic to create websites?

Post by wysardry »

I've read a little more of the manual and am now thinking that using SQLite to store most of the data would be a good move.

I haven't yet decided whether to store the HTML for each post in the database too, as I'm not sure whether people would need an option to edit them in an external HTML editor. I do want the page templates to be actual files though, as creating or modifying "themes" would be easier that way.

Is it possible to create complex editor interfaces in PureBasic? I'm thinking of something along the lines of Bluefish or Thingamablog as writing posts and interacting with a database via a command line program would be a pain.

The closest thing I've done before is creating online form interfaces in Perl and HTML.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Using PureBasic to create websites?

Post by Lunasole »

wysardry wrote: Is it possible to create complex editor interfaces in PureBasic? I'm thinking of something along the lines of Bluefish or Thingamablog as writing posts and interacting with a database via a command line program would be a pain.

The closest thing I've done before is creating online form interfaces in Perl and HTML.
Purebasic IDE itself is made in PB (with it's quite complex UI).
The other most complex UI I've seen on PB is example by Andre, included into this stuff ( http://www.purebasic.fr/english/viewtop ... 12&t=65330 )

Or saying shortly, surely that's possible ^^ But using for that raw built-in library and form designer probably will be painful.
At least I've quickly tired from that and created also dialogs-based stuff for myself
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Using PureBasic to create websites?

Post by wysardry »

Thanks. The Dynamic Dialogues module in that thread looks like it would make the process easier. I'm not sure if any of my screens will need to have quite so many different types of input areas as in Andre's example, but overall the total might well be similar.

I haven't yet decided whether to include a WYSIWYG HTML editor screen, as I've always found them to be restrictive, but buttons to insert a range of different HTML tags would be useful.

I'm guessing that PureBasic doesn't have any HTML code syntax highlighting options as standard.
Post Reply