www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D Tutorials

reply J C Calvarese <jcc7 cox.net> writes:
Before someone has a chance to ask, I figured I'd provide some helpful 
links...


*The nexus of D tutorials*
http://www.prowiki.org/wiki4d/wiki.cgi?D__Tutorial

There are some subpages with help for beginners, but it's not much. Yet. 
I plan to add some more subpages, but I haven't gotten around to it yet. 
Hopefully, I'll find some time for it in the next couple months. If 
someone else gets tired of waiting for me, please be my guest. Don't let 
me stop you. ;)


*dsource tutorials*
Example database: http://www.dsource.org/tutorials/
Forum:            http://www.dsource.org/forums/viewforum.php?f=3

I (mostly) wrote the PHP that runs the tutorial section at dsource. If 
you have an idea for a new feature, I'm the one to whom you should 
mention it. I've written some of the examples, but many have been 
submitted by others or culled from the D newsgroups.

The best way to get my attention regarding a tutorial issue is probably 
either by posting in the tutorials forum or by sending me a PM (I'm 
"jcc7") through the dsource forums. In the case that you find a problem 
with an existing example, Brad and I are the only ones who can edit it. 
So just let me know, and I'll fix it.

-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
Mar 16 2005
next sibling parent reply "Joey Peters" <squirrel nidhogg.com> writes:
 I'm interested in writing some tutorials, just to review my current D 
skills. I've been using D for a few months now, I should be able to write a 
few beginner tutorials.
Oh, just as a little side question; do you know what exactly the 
in/out/inout parameter options mean? I find the reference explanation a bit 
confusing.


"J C Calvarese" <jcc7 cox.net> schreef in bericht 
news:d1aob8$2kq9$1 digitaldaemon.com...
 Before someone has a chance to ask, I figured I'd provide some helpful 
 links...


 *The nexus of D tutorials*
 http://www.prowiki.org/wiki4d/wiki.cgi?D__Tutorial

 There are some subpages with help for beginners, but it's not much. Yet. I 
 plan to add some more subpages, but I haven't gotten around to it yet. 
 Hopefully, I'll find some time for it in the next couple months. If 
 someone else gets tired of waiting for me, please be my guest. Don't let 
 me stop you. ;)


 *dsource tutorials*
 Example database: http://www.dsource.org/tutorials/
 Forum:            http://www.dsource.org/forums/viewforum.php?f=3

 I (mostly) wrote the PHP that runs the tutorial section at dsource. If you 
 have an idea for a new feature, I'm the one to whom you should mention it. 
 I've written some of the examples, but many have been submitted by others 
 or culled from the D newsgroups.

 The best way to get my attention regarding a tutorial issue is probably 
 either by posting in the tutorials forum or by sending me a PM (I'm 
 "jcc7") through the dsource forums. In the case that you find a problem 
 with an existing example, Brad and I are the only ones who can edit it. So 
 just let me know, and I'll fix it.

 -- 
 Justin (a/k/a jcc7)
 http://jcc_7.tripod.com/d/ 

Mar 17 2005
next sibling parent reply AEon <AEon_member pathlink.com> writes:
"J C Calvarese" <jcc7 cox.net>,

Just had a quick look at teh turorials. Very nice.

in 

http://www.dsource.org/tutorials/index.php?show_example=26

I was a bit surprised that

<code>
void main()
{ 
printf("Hi");
}
</code>

actually works with dmd. I would have guessed it requires

<code>
import std.c.stdio;

void main()
{ 
printf("Hi\n");
}
</code>

an import and a \n. But the example works without these. Strange.

AEon
Mar 17 2005
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
AEon wrote:

 <code>
 import std.c.stdio;
 
 void main()
 { 
 printf("Hi\n");
 }
 </code>
 
 an import and a \n. But the example works without these. Strange.

This "long version" of yours is the correct one... Walter has just added printf to object.d "while debugging Phobos", and printing a newline is the only way to make sure it shows up... Of course, the return value of void is still broken in current DMD. :( (hopefully that will be fixed one day, so that we can use "void main") But the canonical forms are either of:
 import std.stdio;
 void main()
 {
   writefln("Hello, World!");
 }
 

 import std.c.stdio;
 int main()
 {
   printf("Hello, World!\n");
   return 0;
 }

--anders
Mar 17 2005
prev sibling parent J C Calvarese <jcc7 cox.net> writes:
AEon wrote:
 "J C Calvarese" <jcc7 cox.net>,
 
 Just had a quick look at teh turorials. Very nice.
 
 in 
 
 http://www.dsource.org/tutorials/index.php?show_example=26
 
 I was a bit surprised that
 
 <code>
 void main()
 { 
 printf("Hi");
 }
 </code>
 
 actually works with dmd. I would have guessed it requires

Yes, it's magic. :) I'm one of those who is trying to lobby Walter to remove the public import of std.c.stdio from object.d, but until he does so it is legal code. -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Mar 17 2005
prev sibling next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 17 Mar 2005 10:59:59 +0100, Joey Peters wrote:



 Oh, just as a little side question; do you know what exactly the 
 in/out/inout parameter options mean? I find the reference explanation a bit 
 confusing.

 Oh, just as a little side question; do you know what exactly the 
 in/out/inout parameter options mean? I find the reference explanation a bit 
 confusing.

'in' parameters have their value preserved. That is, after calling a routine, the value of an 'in' parameter will not be changed after the routine has returned. The routine can change the value but when the routine returns, the value is restored to whatever it was when the routine was called. There is on tricky part though. Class instances and arrays are passed by reference. That means that a pointer to the data is passed to the array, and in this case an 'in' parameter just means that the pointer value is preserved *and* not the values in the class or array. Example of 'in': import std.stdio; void Foo(in char[] x) { writefln("Foo Before '%s' %d", x, x.length); if (x.length == 0) x ~= 'A'; else x[0] = 'A'; x = "xyzzy"; writefln("Foo After '%s' %d", x, x.length); } void main() { char[] Test = "abcdef"; writefln("Main Before '%s' %d", Test, Test.length); Foo(Test); writefln("Main After '%s' %d", Test, Test.length); } --------------------------- Main Before 'abcdef' 6 Foo Before 'abcdef' 6 Foo After 'xyzzy' 5 Main After 'Abcdef' 6 ------------------------ 'out' parameters are initialized by D to whatever their .init property is at the point they are passed to the routine. The routine may change the values in 'out' parameters and the changes remain after the routine returns. Example of 'out': import std.stdio; void Foo(out char[] x) { writefln("Foo Before '%s' %d", x, x.length); if (x.length == 0) x ~= 'A'; else x[0] = 'A'; x = "xyzzy"; writefln("Foo After '%s' %d", x, x.length); } void main() { char[] Test = "abcdef"; writefln("Main Before '%s' %d", Test, Test.length); Foo(Test); writefln("Main After '%s' %d", Test, Test.length); } --------------------------------- Main Before 'abcdef' 6 Foo Before '' 0 Foo After 'xyzzy' 5 Main After 'xyzzy' 5 --------------------------------- 'inout' parameters are identical to 'out' parameters except that they are *not* initialized by D. The enter the routine with what ever value was in them when passed to the routine. Example of 'inout': import std.stdio; void Foo(inout char[] x) { writefln("Foo Before '%s' %d", x, x.length); if (x.length == 0) x ~= 'A'; else x[0] = 'A'; x = "xyzzy"; writefln("Foo After '%s' %d", x, x.length); } void main() { char[] Test = "abcdef"; writefln("Main Before '%s' %d", Test, Test.length); Foo(Test); writefln("Main After '%s' %d", Test, Test.length); } --------------------------------- Main Before 'abcdef' 6 Foo Before 'abcdef' 6 Foo After 'xyzzy' 5 Main After 'xyzzy' 5 --------------------------------- -- Derek Parnell Melbourne, Australia 17/03/2005 9:34:49 PM
Mar 17 2005
parent "Joey Peters" <squirrel nidhogg.com> writes:
I think I understand how it works now, thanks.

"Derek Parnell" <derek psych.ward> schreef in bericht 
news:eg34k000ed53$.45l1iy5k4huv$.dlg 40tude.net...
 On Thu, 17 Mar 2005 10:59:59 +0100, Joey Peters wrote:



 Oh, just as a little side question; do you know what exactly the
 in/out/inout parameter options mean? I find the reference explanation a 
 bit
 confusing.

 Oh, just as a little side question; do you know what exactly the
 in/out/inout parameter options mean? I find the reference explanation a 
 bit
 confusing.

'in' parameters have their value preserved. That is, after calling a routine, the value of an 'in' parameter will not be changed after the routine has returned. The routine can change the value but when the routine returns, the value is restored to whatever it was when the routine was called. There is on tricky part though. Class instances and arrays are passed by reference. That means that a pointer to the data is passed to the array, and in this case an 'in' parameter just means that the pointer value is preserved *and* not the values in the class or array. Example of 'in': import std.stdio; void Foo(in char[] x) { writefln("Foo Before '%s' %d", x, x.length); if (x.length == 0) x ~= 'A'; else x[0] = 'A'; x = "xyzzy"; writefln("Foo After '%s' %d", x, x.length); } void main() { char[] Test = "abcdef"; writefln("Main Before '%s' %d", Test, Test.length); Foo(Test); writefln("Main After '%s' %d", Test, Test.length); } --------------------------- Main Before 'abcdef' 6 Foo Before 'abcdef' 6 Foo After 'xyzzy' 5 Main After 'Abcdef' 6 ------------------------ 'out' parameters are initialized by D to whatever their .init property is at the point they are passed to the routine. The routine may change the values in 'out' parameters and the changes remain after the routine returns. Example of 'out': import std.stdio; void Foo(out char[] x) { writefln("Foo Before '%s' %d", x, x.length); if (x.length == 0) x ~= 'A'; else x[0] = 'A'; x = "xyzzy"; writefln("Foo After '%s' %d", x, x.length); } void main() { char[] Test = "abcdef"; writefln("Main Before '%s' %d", Test, Test.length); Foo(Test); writefln("Main After '%s' %d", Test, Test.length); } --------------------------------- Main Before 'abcdef' 6 Foo Before '' 0 Foo After 'xyzzy' 5 Main After 'xyzzy' 5 --------------------------------- 'inout' parameters are identical to 'out' parameters except that they are *not* initialized by D. The enter the routine with what ever value was in them when passed to the routine. Example of 'inout': import std.stdio; void Foo(inout char[] x) { writefln("Foo Before '%s' %d", x, x.length); if (x.length == 0) x ~= 'A'; else x[0] = 'A'; x = "xyzzy"; writefln("Foo After '%s' %d", x, x.length); } void main() { char[] Test = "abcdef"; writefln("Main Before '%s' %d", Test, Test.length); Foo(Test); writefln("Main After '%s' %d", Test, Test.length); } --------------------------------- Main Before 'abcdef' 6 Foo Before 'abcdef' 6 Foo After 'xyzzy' 5 Main After 'xyzzy' 5 --------------------------------- -- Derek Parnell Melbourne, Australia 17/03/2005 9:34:49 PM

Mar 17 2005
prev sibling parent J C Calvarese <jcc7 cox.net> writes:
Joey Peters wrote:
  I'm interested in writing some tutorials, just to review my current D 
 skills. I've been using D for a few months now, I should be able to write a 
 few beginner tutorials.

Great! I know that there's a lot more we could do to explain D to newcomers. Many of the same questions seem to always come up with someone who is new to D.
 "J C Calvarese" <jcc7 cox.net> schreef in bericht 
 news:d1aob8$2kq9$1 digitaldaemon.com...
 
Before someone has a chance to ask, I figured I'd provide some helpful 
links...


*The nexus of D tutorials*
http://www.prowiki.org/wiki4d/wiki.cgi?D__Tutorial


-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Mar 17 2005
prev sibling next sibling parent reply AEon <AEon_member pathlink.com> writes:
In article J C Calvarese says...

*dsource tutorials*
Example database: http://www.dsource.org/tutorials/
Forum:            http://www.dsource.org/forums/viewforum.php?f=3

I (mostly) wrote the PHP that runs the tutorial section at dsource. If 
you have an idea for a new feature, I'm the one to whom you should 
mention it.

A few ideas: Example thread: http://www.dsource.org/tutorials/index.php?show_example=68 On the "Add an example" link page (http://www.dsource.org/tutorials/index.php?add_example=1) - It would be nice if the category section would "inherit" the category from which you clicked that link. In the above case it would be "Fundamentals". - To avoid inadvertent "mess ups" it could be nice to have a drop-down list of the valid "Category:"s. AFAICT those are 3 presently? Fundamentals, Intermediate, Advanced. - Under the "Submit" button, it would really be nice to have a "Preview" button, because AFAICT there is no way to edit posts to correct layout bugs etc. IMO this would be one of the most important additions, since nice formatting greatly improves readability + understanding of code. - In the thread above I noted the source code is automatically highlighted? I have put together a highlighting file for UltraEdit, that could possibly be used by PHP to make the source code even more readable. E.g. highlight keywords, properties, operators, comments. It has been a while, but did not PHP have some sort of highlighting feature built in, or was that got HTML... hmmm :) - This is probably not the way "newsgroups"-like sites work. But if you added accounts, not only would the authors have it easier to add content. It would also be a "guaranteed" way to contact them. Furthermore this could allow authors to "edit" and "fix" their posts. If you want arbitrary editing to be avoided?! Just some thoughts :) You can contact me either here in this newsgroup, or email aeon <(-a t-)> planetquake (<d o t>) com. AEon
Mar 17 2005
parent reply J C Calvarese <jcc7 cox.net> writes:
In article <d1bogs$lr7$1 digitaldaemon.com>, AEon says...
In article J C Calvarese says...

*dsource tutorials*
Example database: http://www.dsource.org/tutorials/
Forum:            http://www.dsource.org/forums/viewforum.php?f=3


- It would be nice if the category section would "inherit" the category from
which you clicked that link. In the above case it would be "Fundamentals".

Yes, that's a good idea. I've thought the same thing a few times. I can't remember why I haven't implemented that yet. I guess I just haven't gotten around to it yet.
- To avoid inadvertent "mess ups" it could be nice to have a drop-down list of
the valid "Category:"s. AFAICT those are 3 presently? Fundamentals,
Intermediate, Advanced.

There are many categories and I'd like you contributors to be able to add their own. I think implementing your first suggestion would lessen the need for doing something like this (since I don't know that I could get this to work the way I'd like it to work).
- Under the "Submit" button, it would really be nice to have a "Preview" button,
because AFAICT there is no way to edit posts to correct layout bugs etc.

IMO this would be one of the most important additions, since nice formatting
greatly improves readability + understanding of code.

I've also thought about doing this. It shouldn't be too hard to do, so maybe I'll add this soon, too.
- In the thread above I noted the source code is automatically highlighted? I
have put together a highlighting file for UltraEdit, that could possibly be used
by PHP to make the source code even more readable. E.g. highlight keywords,
properties, operators, comments. It has been a while, but did not PHP have some
sort of highlighting feature built in, or was that got HTML... hmmm :)

I ported a program called d2html (my improved version of Pavel's original code) to PHP. It works pretty well, but I admit it's not flawless.
- This is probably not the way "newsgroups"-like sites work. But if you added
accounts, not only would the authors have it easier to add content. It would
also be a "guaranteed" way to contact them. Furthermore this could allow authors
to "edit" and "fix" their posts. If you want arbitrary editing to be avoided?!

Maybe I could also the submitter to edit his own example. I'll think about this idea.
Just some thoughts :)

Thanks. You've brought up a lot of good points. I don't know how much time I have to work on this in the month or so, but maybe I'll have a chance to implement some of this before May. ;) jcc7
Mar 17 2005
parent reply Brad Anderson <brad dsource.dot.org> writes:
J C Calvarese wrote:
<snip>

 Thanks. You've brought up a lot of good points. I don't know how much time I
 have to work on this in the month or so, but maybe I'll have a chance to
 implement some of this before May. ;)
 
 jcc7

Justin, We should talk. The tutorial section of dsource.org could be a plugin/module of Trac. I.E. each project could have a tutorial section, along with forums, tickets, downloads, news, etc. Then we could have a project at dsource that is kind of like the "D Language" project. People could add their PEP-like requests, have tickets to track bugs, and all the tutorials for the language in general, and not a specific project, could be in this "D Language" project. I'm bringing it up, because if we want to integrate with Trac, you'd have to rewrite in Python and use ClearSilver templates, instead of PHP. Obviously, we'd convert the current ones to the new system... Let's discuss, either here, on the site forum at dsource.org, or offline. BA
Mar 17 2005
parent reply J C Calvarese <jcc7 cox.net> writes:
Brad Anderson wrote:
 Justin,
 
 We should talk.  The tutorial section of dsource.org could be a 
 plugin/module of Trac.  I.E. each project could have a tutorial section, 
 along with forums, tickets, downloads, news, etc.

I think we already discussed some of this. I managed to install Trac onto my local machine, and I saw how syntax highlighing could be handled by writing a Python function. I just haven't gotten around to porting d2html to Python yet. It shouldn't be a problem to use Trac wiki pages (with syntax highlighing) for the tutorial examples. I'd rather have wiki pages to track changes than lock most people out (like we're currently doing).
 Then we could have a project at dsource that is kind of like the "D 
 Language" project.  People could add their PEP-like requests, have 
 tickets to track bugs, and all the tutorials for the language in 
 general, and not a specific project, could be in this "D Language" project.

That sounds fine to me.
 I'm bringing it up, because if we want to integrate with Trac, you'd 
 have to rewrite in Python and use ClearSilver templates, instead of PHP. 
  Obviously, we'd convert the current ones to the new system...  Let's 
 discuss, either here, on the site forum at dsource.org, or offline.

Will the new server still have PHP? I think it's a good idea to make use of Trac for the tutorials in the near future, but I doubt I'll be ready before you're ready to go live with "dsource 2.0". ;) -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Mar 17 2005
parent reply J C Calvarese <jcc7 cox.net> writes:
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

J C Calvarese wrote:
 Brad Anderson wrote:
 
 Justin,

 We should talk.  The tutorial section of dsource.org could be a 
 plugin/module of Trac.  I.E. each project could have a tutorial 
 section, along with forums, tickets, downloads, news, etc.


...
 Will the new server still have PHP? I think it's a good idea to make use 
 of Trac for the tutorials in the near future, but I doubt I'll be ready 
 before you're ready to go live with "dsource 2.0". ;)

Sometimes, I'm my own harshest critic. It wasn't nearly as hard to convert the PHP to Python as I was thinking it might be. I'm not saying it's perfect, but I think it's already nearly as good as the PHP version. It seems to work great on the Trac system I have running locally. You should place the attached file in \pythonXX\lib\site-packages\trac\wikimacros\ where "pythonXX" is your Python directory. Here's some wiki code to test the D processor: {{{ #!d int main(char[] args) { printf("Hello World\n"); return 0; } }}} If it's working, the keywords (int, char, and return) will turn blue, etc. For more information about how this works: http://projects.edgewall.com/trac/wiki/WikiProcessors -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Mar 18 2005
parent reply Brad Anderson <brad dsource.dot.org> writes:
Looks great.  I'll check it out tomorrow.  Thanks!!

Did you think about integrating with the 3rd party tools here:
http://projects.edgewall.com/trac/wiki/TracSyntaxColoring

This would extend the syntax highlighting to the source-code browser 
instead of just Wiki pages.  Although it would need to be in Scintilla 
for SilverCity.  That's C++ (ick) instead of Python...

Still, it's cool progress, and I need a jump-start.

BA


J C Calvarese wrote:
 J C Calvarese wrote:
 
 Brad Anderson wrote:

 Justin,

 We should talk.  The tutorial section of dsource.org could be a 
 plugin/module of Trac.  I.E. each project could have a tutorial 
 section, along with forums, tickets, downloads, news, etc.


...
 Will the new server still have PHP? I think it's a good idea to make 
 use of Trac for the tutorials in the near future, but I doubt I'll be 
 ready before you're ready to go live with "dsource 2.0". ;)

Sometimes, I'm my own harshest critic. It wasn't nearly as hard to convert the PHP to Python as I was thinking it might be. I'm not saying it's perfect, but I think it's already nearly as good as the PHP version. It seems to work great on the Trac system I have running locally. You should place the attached file in \pythonXX\lib\site-packages\trac\wikimacros\ where "pythonXX" is your Python directory. Here's some wiki code to test the D processor: {{{ #!d int main(char[] args) { printf("Hello World\n"); return 0; } }}} If it's working, the keywords (int, char, and return) will turn blue, etc. For more information about how this works: http://projects.edgewall.com/trac/wiki/WikiProcessors ------------------------------------------------------------------------ # d.py #Place in \pythonXX\lib\site-packages\trac\wikimacros\ import re # D keywords # from http://www.digitalmars.com/d/lex.html#keyword # list checked 2005/03/18 keywords = ['abstract', 'alias', 'align', 'asm', 'assert', 'auto', 'bit', 'body', 'break', 'byte', 'case', 'cast', 'catch', 'cdouble', 'cent', 'cfloat', 'char', 'class', 'const', 'continue', 'creal', 'dchar', 'debug', 'default', 'delegate', 'delete', 'deprecated', 'do', 'double', 'else', 'enum', 'export', 'extern', 'false', 'final', 'finally', 'float', 'for', 'foreach', 'function', 'goto', 'idouble', 'if', 'ifloat', 'import', 'in', 'inout', 'int', 'interface', 'invariant', 'ireal', 'is', 'long', 'mixin', 'module', 'new', 'null', 'out', 'override', 'package', 'pragma', 'private', 'protected', 'public', 'real', 'return', 'short', 'static', 'struct', 'super', 'switch', 'synchronized', 'template', 'this', 'throw', 'true', 'try', 'typedef', 'typeid', 'typeof', 'ubyte', 'ucent', 'uint', 'ulong', 'union', 'unittest', 'ushort', 'version', 'void', 'volatile', 'wchar', 'while', 'with'] # Regex Links # http://www.amk.ca/python/howto/regex/regex.html#SECTION000310000000000000000 # http://docs.python.org/lib/matching-searching.html def dIsAlpha(c): # /* true if c is a letter or an underscore, false otherwise */ return c.isalpha() or (c == '_') def dIsDigit(c): # /* true if c is a number or an underscore, false otherwise */ return c.isdigit() or (c == '_') def ishexdigit(c): # /* true if c is a hexadecimal digit, false otherwise */ return re.compile("^[0-9A-F_]+$").match(c) def isoctdigit(c): # /* true if c is an octal digit, false otherwise */ return re.compile("^[0-7_]+$").match(c) def issymbol(c): # /* true if c is legal D symbol, false otherwise */ # /* "-" also needs to be escaped (in addition to the list at http://www.amk.ca/python/howto/regex/)? */ return re.compile(r"^[?~:|!%{}\\\(\)\[\]\.,;=<>\+\-\*/&\^]+$").match(c) def iskeyword(token): #, keywords): #/* true if token is a D keyword, false otherwise */ for x in keywords: #(i = 0; i < count(keywords.length; $i++) /* keywords.length*/ if (x == token): return True return False class Stream: # /* This somewhat emulates Phobos's Stream class, so I didn't have to completely rewrite d2html. */ pos = 0 src = "" def position(self): return self.pos def read(self): self.pos = self.pos + 1 if(self.pos < len(self.src) + 1): return self.src[self.pos - 1 : self.pos] else: return " " def write(self, c): self.src = self.src + c def writeString(self, c): self.src = self.src + c def writeLine(self, c): self.src = self.src + c + "\n" # /* append with a newline */ def __init__(self, s): self.src = s self.pos = 0 def execute(hdf, text, env): """ This function is called when code is embedded in a trac wiki page: {{{ #!d int main(char[] args) { printf("Hello World\n"); return 0; } }}} The "d" indicates to use the D processor that this file contains. For more information, see http://projects.edgewall.com/trac/wiki/WikiProcessors. """ # hdf is an HDFObjectType (and can't be concatenated with a string) # env is an instance (and can't be concatenated with a string) return d2html(text) def d2html(raw_code): # * Copyright (c) 2001 # * Pavel "EvilOne" Minayev # * # * Permission to use, copy, modify, distribute and sell this software # * and its documentation for any purpose is hereby granted without fee, # * provided that the above copyright notice appear in all copies and # * that both that copyright notice and this permission notice appear # * in supporting documentation. Author makes no representations about # * the suitability of this software for any purpose. It is provided # * "as is" without express or implied warranty. # * Updated by J C Calvarese, http://jcc_7.tripod.com/d/, 2003/12/18 # * Ported to Python by J C Calvarese, 2005/03 # ### Porting Tips ### # Concatenation: # D: ~ # PHP: . # Python: + # PHP do-while Python equivalent # ------------ ----------------- # doIt = True # do { while(doIt): # ... ... # if not(condition): # } while (condition) doIt = False # PHP for Python equivalent # ------- ----------------- # for (i = 0; i < spaces; i++) for i in range(spaces): # By the way, string methods come in handy when doing text processing: # http://docs.python.org/lib/string-methods.html code = raw_code #/* Remove trailing CR and/or LF (doesn't seem to work, but doesn't seem to hurt either). */ done = False while done == False: iLen = len(code) code = code.rstrip() if iLen == len(code): done = True tabsize = 4 #/* number of spaces in tab */ lineNumber = 0 spaces = 0 #/* Colors for syntax highlighting, default values are # Pavel Minayev's preferences in Microsoft Visual Studio editor */ Colors_keyword = "0000FF" Colors_number = "008000" Colors_string = "000080" Colors_comment = "808080" #useStyleSheet = True useStyleSheet = False xhtmlFormat = True linestart = 0 #/* for tabs */ c = "" c2 = "" src = Stream(code) dst = Stream("") c = src.read() while src.position() < len(code): if (c.isspace()): #/* whitespace */ doIt = True while(doIt): if c == "\t": #/* tab character */ #/* expand tabs to spaces */ spaces = tabsize - (src.position() - linestart) % tabsize for i in range(spaces): dst.writeString(" ") linestart = src.position() - tabsize + 1 elif c == "\n": #//10: /* linefeed */ linestart = src.position() + 1 #/* reset line start on newline */ dst.write(c) elif c == "\r": #//13: /* control return */ linestart = src.position() + 1 #/* reset line start on newline */ if(c2 != "\n"): #//10) dst.write(c) else: #/* space is left, anything else? */ dst.write(c) c2 = c c = src.read() if not c.isspace(): doIt = False elif (dIsAlpha(c)): #/* keyword or identifier */ token = "" doIt = True while(doIt): token = token + c c = src.read() if not(dIsAlpha(c) or dIsDigit(c)): doIt = False if(iskeyword(token)): #/* keyword */ if(useStyleSheet): dst.writeString('<span class="keyword">' + token + "</span>") else: dst.writeString("<font color='#" + Colors_keyword + "'>" + token + "</font>") else: # /* simple identifier */ dst.writeString(token) elif (c == '0'): #/* binary, octal or hexadecimal number */ if useStyleSheet: dst.writeString('<span class="number">') else: dst.writeString("<font color='#" + Colors_number + "'>") dst.write(c) c = src.read() if c == 'X' or c == 'x': #/* hexadecimal */ dst.write(c) c = src.read() while ishexdigit(c): dst.write(c) c = src.read() # /* TODO: add support for hexadecimal floats */ elif (c == 'B' or c == 'b'): #/* binary */ dst.write(c) c = src.read() while c == '0' or c == '1' or c == '_': dst.write(c) c = src.read() else: #/* octal */ doIt = True while (doIt): dst.write(c) c = src.read() if not(isoctdigit(c)): doIt = False dst.write(c) c = src.read() if useStyleSheet: dst.writeString("</span>") else: dst.writeString("</font>") elif dIsDigit(c): #/* decimal number */ if(useStyleSheet): dst.writeString('<span class="number">') else: dst.writeString("<font color='#" + Colors_number + "'>") #/* integral part */ doIt = True while(doIt): dst.write(c) c = src.read() if not(dIsDigit(c)): doIt = False #/* fractional part */ if c == '.': dst.write(c) c = src.read() while dIsDigit(c): dst.write(c) c = src.read() #/* scientific notation */ if c == 'E' or c == 'e': dst.write(c) c = src.read() if c == '+' or c == '-': dst.write(c) c = src.read() while dIsDigit(c): dst.write(c) c = src.read() #/* suffixes */ while c == 'U' or c == 'u' or c == 'L' or c == 'l' or c == 'F' or c == 'f': dst.write(c) c = src.read() if useStyleSheet: dst.writeString("</span>") else: dst.writeString("</font>") elif c == "\\": #/* naked escape sequence (\) */ if(useStyleSheet): dst.writeString("<span class=\"string\">") else: dst.writeString("<font color='#" + Colors_string + "'>") dst.write(c) prev = c c = src.read() dst.write(c) c = src.read() if useStyleSheet: dst.writeString("</span>") else: dst.writeString("</font>") elif c == "\"": #/* string (") with escape sequences */ if(useStyleSheet): dst.writeString('<span class="string">') else: dst.writeString("<font color='#" + Colors_string + "'>") doIt = True while(doIt): if c == '<': #/* special symbol in HTML */ dst.writeString("&lt;") else: dst.write(c) prev = c if ((prev == '\\' and c == '"') or (prev == '\\' and c == '\\') and not isEscape): isEscape = True else: isEscape = False c = src.read() if not(isEscape or c != '"'): doIt = False dst.write(c) c = src.read() if useStyleSheet: dst.writeString("</span>") else: dst.writeString("</font>") elif c == "'": #/* character (') with escape sequences */ if useStyleSheet: dst.writeString('<span class="string">') else: dst.writeString("<font color='#" + Colors_string + "'>") doIt = True while(doIt): if c == '<': #/* special symbol in HTML */ dst.writeString("&lt;") else: dst.write(c) prev = c if (prev == "\\" and c == "'") or (prev == "\\" and c == "\\") and not isEscape: isEscape = True else: isEscape = False c = src.read() if not(isEscape or c != '\''): doIt = False dst.write(c) c = src.read() if useStyleSheet: dst.writeString("</span>") else: dst.writeString("</font>") elif c == '`': #/* character (`) with no escape sequences */ if useStyleSheet: dst.writeString('<span class="string">') else: dst.writeString("<font color='#" + Colors_string + "'>") doIt = True while(doIt): if (c == '<'): #/* special symbol in HTML */ dst.writeString("&lt;") else: dst.write(c) c = src.read() if c == "`": #/* 96 */ doIt = False dst.write(c) c = src.read() if useStyleSheet: dst.writeString("</span>") else: dst.writeString("</font>") elif issymbol(c): #/* either operator or comment */ if c == '<': #/* special symbol in HTML */ dst.writeString("&lt;") c = src.read() elif c == '/': #/* could be a comment... */ c = src.read() if c == '/': #/* single-line one */ if useStyleSheet: dst.writeString("<span class=\"comment\">/") else: dst.writeString("<font color='#" + Colors_comment + "'>/") while c != "\n" and c != "\r": #//10 and $c != 13: if c == '<': #/* special symbol in HTML */ dst.writeString("&lt;") elif c == "\t": #// 9 #/* expand tabs */ spaces = tabsize - (src.position() - linestart) % tabsize for i in range(spaces): dst.writeString(" ") linestart = src.position() - tabsize + 1 else: dst.write(c) c2 = c c = src.read() if useStyleSheet: dst.writeString("</span>") else: dst.writeString("</font>") elif (c == '*'): #/* multi-line one */ prevprev = "" prev = '/' if useStyleSheet: dst.writeString('<span class="comment">/'); else: dst.writeString("<font color='#" + Colors_comment + "'>/") doIt = True while(doIt): if c == '<': #/* special symbol in HTML */ dst.writeString("&lt;") elif (c == "\t"): #//9) #/* expand tabs */ spaces = tabsize - (src.position() - linestart) % tabsize for i in range(spaces): dst.writeString(" ") linestart = src.position() - tabsize + 1 else: if(c == "\n" or c == "\r"): #//c == 10 || c == 13) linestart = src.position() + 1 #/* reset line start on newline */ dst.write(c) prevprev = prev prev = c c = src.read() if prevprev != '/' and prev == '*' and c == '/': doIt = False dst.write(c) if useStyleSheet: dst.writeString("</span>") else: dst.writeString("</font>") c = src.read() elif (c == '+'): #/* nestable multi-line comment */ prevprev = '/' prev = '+' level = 0 if useStyleSheet: dst.writeString("<span class=\"comment\">/") else: dst.writeString("<font color='#" + Colors_comment + "'>/") doIt = True while(doIt): if (c == '<'): #/* special symbol in HTML */ dst.writeString("&lt;") elif (c == "\t"): #//9) #/* expand tabs */ spaces = tabsize - (src.position() - linestart) % tabsize for i in range(spaces): dst.writeString(" ") linestart = src.position() - tabsize + 1 else: #/* reset line start on newline */ if (c == "\n" or c == "\r"): #//10 || $c == 13) linestart = src.position() + 1 dst.write(c) if (prev == '/' and c == '+'): level = level + 1 if (prevprev != '/' and prev == '+' and c == '/'): level = level - 1 prevprev = prev prev = c c = src.read() if prev == '+' and c == '/' and level == 0: doIt = False dst.write(c) if useStyleSheet: dst.writeString("</span>") else: dst.writeString("</font>") c = src.read() else: #/* just an operator */ dst.write('/') else: #/* just an operator */ dst.write(c) c = src.read() else: #/* whatever it is, it doesn't seem to be valid */ dst.write("{error!}") dst.write(c) strippedSrc = dst.src.strip if(strippedSrc != ""): #/* make sure there's actual content */ return "<pre class=\"code-block\"><code>" + dst.src + "</code></pre>" # class=\"example\" return ""

Mar 18 2005
parent J C Calvarese <jcc7 cox.net> writes:
Brad Anderson wrote:
 Looks great.  I'll check it out tomorrow.  Thanks!!

I hope it works for you too. ;)
 
 Did you think about integrating with the 3rd party tools here:
 http://projects.edgewall.com/trac/wiki/TracSyntaxColoring

Yeah, I've heard of that. I'd be more somewhat more interested if I could write it in D, PHP, Python, BASIC, or Java.
 This would extend the syntax highlighting to the source-code browser 
 instead of just Wiki pages.  Although it would need to be in Scintilla 
 for SilverCity.  That's C++ (ick) instead of Python...

"Ick" is right. I don't really know C++, and I'm not particularly interested in learning it right now. Maybe next year. ;) I don't really know much Python either, but I think that scripting languages make trial-and-error a lot safer (and more effective) than the raw power of (non-garbage-collected) C and C++ would. I remember reading that someone was interested in writing a Scintilla lexer. More power to him if someone wants to do that, but personally I'd prefer to tweak the Python code that we have now than start programming in C++.
 
 Still, it's cool progress, and I need a jump-start.

It ended up being easy to port. When I was working on it back in August, I lost interest when I discovered that Python doesn't have a do-while construct. (Not that I really expected that to be a major problem.) I just felt like shelving the project at that point. Then when I picked it up again the other day, I quickly realized that it was going to be easy to complete it. So I went ahead and finished off the conversion while I was still in a good mood about it. :) -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Mar 18 2005
prev sibling parent AEon <AEon_member pathlink.com> writes:
J C Calvarese says...
*dsource tutorials*
Example database: http://www.dsource.org/tutorials/
Forum:            http://www.dsource.org/forums/viewforum.php?f=3

Sorry about a few error / oversights on my part. E.g. you seem to have *many* categories. Plus these can AFAICT be "freely" extended. Some more thoughts/suggestions: - Highligthing suggestions e.g http://www.dsource.org/tutorials/index.php?show_example=64 - Comments blue green (ie. the color you use for links, no block background color) - String literals colored (no block background), gray or something else less eye-poking. - Operators green - Blue keywords are fine. - Numbers etc. red (everything presently shown in green) Both string literals and comments are too prominant IMO (destracting), making it very hard to read the source code. - The code block framing might be a bit nicer to read by adding a empty 1st and last lines, i.e. blue dotted frame keeping it's distance from all sides of the code. - Pardon my saying so, but after reading some 20 source examples, the dotted frame around the source code is kind of an eye-saw IMO, possibly replace it with a thin solid line, in a darker gray tone to mellow down the visual impact of code blocks frame. - Formatting Turorial? http://www.dsource.org/tutorials/index.php?show_example=15 It would be helpful to have some sort of "formatting" tutorial, how exactly you did the standard test, headlines etc. And how code blocks are inserted. To help folks keep formatting consistent. - http://www.dsource.org/tutorials/index.php?show_example=37 printf("Press a key (using 'std.c.stdio.getch();' to wait) . . .\n\0"); getch(); is the \0 actually correct and/or needed or useful? I have never seen string literals requiring a \0. (is used 3 times in the example). - It could be interesting to allow folks to add examples to existing examples, to avoid the spamming of similar code sniplets. I.e. to have them all in the proper "thread". AEon
Mar 17 2005