Πρότυπο:Κώδικας: Διαφορά μεταξύ των αναθεωρήσεων

Περιεχόμενο που διαγράφηκε Περιεχόμενο που προστέθηκε
αρχίζω τη μετάφρασση σιγά σιγά
(Καμία διαφορά)

Αναθεώρηση της 23:36, 3 Αυγούστου 2008

Πρότυπα Κώδικα

Ο σκοπός αυτής της σειράς προτύπων είναι η διευκόλυνση της ανάγνωσης παραδειγμάτων κώδικα σε βιβλία για τον προγραμματισμό στα Βικιβιβλία. Κἀθε πρότυπο έχει μια ετικέτα <div><nowiki> με έγχρωμο περίγραμμα και έγχρωμο φόντο, και δεύτερη <nowiki><div><nowiki> με έγχρωμο περίγραμμα και άσπρο φόντο. Μέσα στην πρώτη <nowiki><div><nowiki> υπάρχει χώρος για σχόλιο ή συμβουλή, και η δεύτερη περιέχει χώρο για τον κώδικα. Τα πρότυπα παίρνουν (τουλάχιστον) δύο παραμέτρους: το σχόλιο, και τον κώδικα τον ίδιο. Λόγω του τρόπου λειτουργίας του parser του MediaWiki, πρέπει ο χρήστης να βάλει τις ετικέτες <nowiki><pre> και </pre> στην αρχή και στο τέλος του κώδικα. .

Σημειώσεις

Πρέπει ο χρήστης να βάλει το σωστό αριθμό από κάθετες γραμμές | στη κλήση του προτύπου, ακόμα όταν η παράμετρος είναι κενή. Απλώς γράψτε την κάθετη γραμμή χωρίς άλλο κείμενο.

Κατάλογος προτύπων κώδικα

  • Πρότυπο:Κώδικας:Βασικό - Βασικό: δείχνει κώδικα/σύνταξη χωρίς άλλες λειτουργίες (μπλε)
  • Πρότυπο:Κώδικας:Έγκυρος - Έγκυρο: δείχνει έγκυρη κώδικα/σύνταη (πράσινο) με πράσινο εικονίδιο checkmark
  • Πρότυπο:Κώδικας:Σφάλμα - Σφάλμα: δείχνει λανθασμένο κώδικα/σύνταξη (κόκκινο) με κόκκινο εικονίδιο x-mark
  • Πρότυπο:Κώδικας:Εναλλ - Εναλλ (Εναλλακτικό): δείχνει δύο δείγματα κώδικα με «ή» ή «και» ως σύνδεσμο (μπλε), θέλει 2 επιπλέον παραμέτρους, το σύδεσμο και το δεύτερο δείγμα κώδικα
  • Πρότυπο:Κώδικας:Έξοδος - Έξοδος: δείχνει κώδικα και έξοδο που παράγεται από τον κώδικα (μπλε) - θέλει 1 επιπλέον παράμετρο, την έξοδο
  • Πρότυπο:Κώδικας:Διερμηνευμένο - Διερμηνευμένο: δείχνει δείγμα μιας διερμηνευμένης γλώσσας από συνέδρια σε τερματικό (μπλε), θέλει από 2 έως 6 παραμέτρους, δείτε τα παραδείγματα παρακάτω
  • Πρότυπο:Κώδικας:PHPHTML - PHP και HTML: δείχνει κώδικα PHP/σύνταξη, την αντίστοιχη έξοδο από την PHP, και βασική εμφάνιση στην HTML (μπλε), θέλει 2 επιπλέον παραμέτρους, την έξοδο σε κανονική μορφή και την έξοδο σε μορφή HTML
  • Πρότυπο:Κώδικας:Συμβουλή - Συμβουλή: δείχνει χρήσιμη συμβουλή για σύνταξη κώδικα με κώδικα/σύνταξη (κίτρινο), με κίτρινο εικονίδιο αστεράκι
  • Πρότυπο:Κώδικας:Διάφορα - Διάφορα: δείχνει άλλο παράδειγμα με κώδικα εκτός από τα παραπάνω (γκρίζο)
  • Πρότυπο:Κώδικας:Διάφορα2 - Διάφορα: δείχνει άλλο παράδειγμα με κώδικα εκτός από τα παραπάνω (άσπρο)
  • Πρότυπο:Κώδικας:Transclude - Transclude: για την transclusion ενός προτύπου κώδικα, με εμφάνιση όμορφου τίτλου (δείτε παρακάτω)

Example Template Usage

Code:Basic

Αυτό το κουτάκι μπορεί να χρησιμοποιηθεί για να εμφανίσει κώδικα ή απλά για να επισημάνει κάποιο κείμενο με μπλε περίγραμμα.
Pie
  1. Cherry
  2. Apple
  3. Peach
$array_pie = {Cherry, Apple, Peach};
Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.


Ο κώδικας για το παραπάνω κουτάκι κώδικα έχει ως εξής:

{{Code:Basic
|Αυτό το κουτάκι μπορεί να χρησιμοποιηθεί για να εμφανίσει κώδικα ή απλά για να επισημάνει κάποιο κείμενο με μπλε περίγραμμα.
|Pie
#Cherry
#Apple
#Peach
<source lang="perl">$array_pie = {Cherry, Apple, Peach};</source>
|Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.
}}

Code:Valid

Ο ακόλουθος κώδικας είναι έγκυρος:
$valid code example;
Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.


Ο κώδικας για το παραπάνω κουτάκι κώδικα έχει ως εξής:

{{Code:Valid
|Ο ακόλουθος κώδικας είναι έγκυρος:
|<pre>$valid code example;</pre>
|Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.
}}

Code:Error

Ο ακόλουθος κώδικας έχει σφάλμα:
$this line does not end with a semicolon
Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.


Ο κώδικας για το παραπάνω κουτάκι κώδικα έχει ως εξής:

{{Code:Error
|Ο ακόλουθος κώδικας έχει σφάλμα:
|<pre>$this line does not end with a semicolon</pre>
|Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.
}}

Code:Alt

You can use either "echo" or "print" to output text.
echo "Hello World.";

and / or / not / etc.

print "Hello World.";
Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.


Ο κώδικας για το παραπάνω κουτάκι κώδικα έχει ως εξής:

{{Code:Alt
|You can use either "echo" or "print" to output text.
|<pre>echo "Hello World.";</pre>
|and / or / not / etc.
|<pre>print "Hello World.";</pre>
|Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.
}}

Code:Output

In this example, you'll be able to see the code and the output it generates.

Κώδικας:

print "Hello World.";

Έξοδος:

Hello World.
Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.


Ο κώδικας για το παραπάνω κουτάκι κώδικα έχει ως εξής:

{{Code:Output
|In this example, you'll be able to see the code and the output it generates.
|<pre>print "Hello World.";</pre>
|<pre>Hello World.</pre>
|Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.
}}

Code:Interpreted

This is a rather more complex code template, intended to present code that can be typed in to an interpreter, rather than compiled. It is expected that this sort of code will be copied and pasted directly into an interactive shell-like environment. However, such code is usually presented with an input prompt before each line, which should not be pasted. This template achieves this by requiring the user to add a few user CSS lines, as described below. The same template can also be used for a variety of formats, such as basic input, or input plus output. This is achieved by using a template that accepts a variable number of arguments.

Named arguments

Called with 4 unnamed arguments, the output generated looks something like Template:Code:Output

The 1st argument is for the initial explanation. The entire box is contained in a div of class "code".

Input:

The 2nd argument is the text typed into the interpreter. It is contained in a div of class "input". Normally the contents should be enclosed in one or more <pre> </pre> or <source> </source> tags, like so
cd /bin #a comment
ls

Result:

The 3rd argument is the output. This should be in <pre> </pre> tags: you probably don't want to use <source>, because syntax-highlighting the output would be silly e.g.
>cd /bin
>ls
bash            df              ln              rm
cat             echo            ls              rmdir
chmod           ed              mkdir           sh
cp              expr            mv              sleep
csh             hostname        ps              stty
date            kill            pwd             sync
dd              ksh             rcp             tcsh

and the 4th argument is the continuation of the explanation. The main use of this template is to produce input text with an auto-generated prompt character, using the :before pseudo-element available in CSS. This has one major advantage, and another possible benefit

  • When copying and pasting the text, the prompt character is not copied in most browsers. This allows the user to copy the text and paste it in to the interpreter verbatim, without removing the prompt
  • The prompt character is user-defined, so can be set to the user's personal prompt character, if desired. This is of dubious benefit.
To see this in action, you need to add something like the following to your user CSS file (usually located at Special:Mypage/monobook.css)
.code .input pre{padding:0; border: none;}
.code .input pre:before {content: "\003E \0020";}
This will put a "> " prompt in front of each <pre> tag in the input box.


Called with three unnamed arguments, the Results box vanishes, as does the (now unneeded) "Input" heading

This is a script in Python. If you wish to format Python scripts differently from other scripts, add the following to your user CSS file
.code * pre.source-python:before, .code .source-python pre:before {content: "\003E \003E \003E \0020";}
hello = "Hello, World!"
print hello #does what it says
See?


However, you might want an Input/Results box with no end explanation. If so, just use a blank 4th argument

You might want to provide a Python-specific prompt in the output too. If so, just call <pre> with the appropriate class

Input:

hello = "Hello, World!"
print hello #does what it says

Result:

hello = "Hello, World!"
print hello #does what it says
Hello, World!


Now it gets simpler. With two unnamed arguments:

There is no "after" text.
cd /bin
ls


With one unnamed argument

there is no before text. The first argument is the code
cd /bin
ls


Named arguments

There are 2 named arguments: "title", "lang". (Note it would be nice to have a 3rd, whether or not to apply an output prompt)

The effect of the "title" argument should be obvious:

A simple example with 2 unnamed arguments and a "title" argument set to "Title example".
Title example
cd /bin
ls


The named argument "lang" is a shortcut, so that you don't need to put the class="source-python" or equivalent in every <pre> tag.

An example with all 6 possible arguments. The "lang" argument adds another class to the input div, called source-"lang"
A title

Input:

hello = "Hello, World!" #syntax highlighted
print hello #not syntax highlighted

Result:

hello = "Hello, World!"
print hello #does what it says
much easier, if you don't want to use <source>.


Wiki Code

The code for all the above codeboxes is

Called with 4 unnamed arguments, the output generated looks something like Template:Code:Output
{{Code:Interpreted
|The 1st argument is for the initial explanation. The entire box is contained in a div of class "code".
|The 2nd argument is the text typed into the interpreter. It is contained in a div of class "input". Normally the contents should be enclosed in one or more <pre> </pre> or <source> </source> tags, like so
<source lang="bash">cd /bin #a comment</source><pre>ls</pre>
|The 3rd argument is the output. This should be in <pre> </pre> tags: you probably don't want to use <source>, because syntax-highlighting the output would be silly e.g.
<pre>>cd /bin</pre>
<pre>>ls
bash            df              ln              rm
cat             echo            ls              rmdir
chmod           ed              mkdir           sh
cp              expr            mv              sleep
csh             hostname        ps              stty
date            kill            pwd             sync
dd              ksh             rcp             tcsh
</pre>
|and the 4th argument is the continuation of the explanation. 
The main use of this template is to produce input text with an auto-generated prompt character, using the :before pseudo-element available in [[CSS]]. This has one major advantage, and another possible benefit
*When copying and pasting the text, the prompt character is not copied in most browsers. This allows the user to copy the text and paste it in to the interpreter verbatim, without removing the prompt
*The prompt character is user-defined, so can be set to the user's personal prompt character, if desired. This is of dubious benefit.
To see this in action, you need to add something like the following to your [[Help:User style |user CSS]] file (usually located at [[Special:Mypage/monobook.css]])<pre>.code .input pre{padding:0; border: none;}
.code .input pre:before {content: "\003E \0020";}</pre>
This will put a " >" prompt in front of each <pre> tag in the input box.
}}

Called with three unnamed arguments, the Results box vanishes, as does the (now unneeded) "Input" heading
{{Code:Interpreted
|This is a script in Python. If you wish to format Python scripts differently from other scripts, add the following to your user CSS file<pre>.code .input pre.source-python:before, .code .source-python pre:before {content: "\003E \003E \003E \0020";}</pre>
|<source lang="python">hello = "Hello, World!"</source>
<source lang="python">print hello #does what it says</source>
|See?
}}

However, you might want an Input/Results box with no end explanation. If so, just use a blank 4th argument
{{Code:Interpreted
|You might want to provide a Python-specific prompt in the output too. If so, just call <pre> with the appropriate class
|<source lang="python">hello = "Hello, World!"</source>
<source lang="python">print hello #does what it says</source>
|<pre class="source-python">hello = "Hello, World!"</pre>
<pre class="source-python">print hello #does what it says
Hello, World!</pre>
|
}}

Now it gets simpler. With two unnamed arguments: 
{{Code:Interpreted
|There is no "after" text.
|<pre>cd /bin</pre><pre>ls</pre>
}}

With one unnamed argument
{{Code:Interpreted
|there is no before text. The first argument is the code<pre>cd /bin</pre><pre>ls</pre>
}}

There are 2 named arguments: "title" and "lang". The effect of the "title" argument should be obvious:
{{Code:Interpreted
|title=Title example
|A simple example with 2 unnamed arguments and a "title" argument set to "Title example".
|<pre>cd /bin</pre><pre>ls</pre>
}}

The named argument "lang" is a shortcut, so that you don't need to put the class="source-python" or equivalent in every <pre> tag. 
{{Code:Interpreted
|A simple example with 3 unnamed arguments and a "lang" argument. The "lang" argument adds another class to the input div, called source-"lang"
|<pre>hello = "Hello, World!"</pre>
<pre>print hello #does what it says</pre>
|much easier, if you don't want to use <source>.
|code=python
}}

Code:PHPHTML

This sample shows you the difference between PHP source, PHP output, and rendered HTML.

PHP κώδικας:

<?php
  $output = "Hello World.";
  print '<html><body>\n';
  print '$output\n';
  print '</body></html>';
?>

PHP έξοδος:

<html><body>
Hello World.
</body></html>

HTML εμφάνιση:

Hello World.
Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.


Ο κώδικας για το παραπάνω κουτάκι κώδικα έχει ως εξής:

{{Code:PHPHTML
|This sample shows you the difference between PHP source, PHP output, and rendered HTML.
|<pre><?php
  $output = "Hello World.";
  print '<html><body>\n';
  print '$output\n';
  print '</body></html>';
?></pre>
|<pre><html><body>
Hello World.
</body></html></pre>
|<pre>Hello World.</pre>
|Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.
}}

Code:Tip

It's a good idea to add comments to your code.
//This variable controls the loop
$loopvar1;
Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικας.


Ο κώδικας για το παραπάνω κουτάκι κώδικα έχει ως εξής:

{{Code:Tip
|It's a good idea to add comments to your code.
|<pre>//This variable controls the loop
$loopvar1;</pre>
|Μπορεί η εξήγηση να συνεχίζεται μετά από το δείγμα κώδικα.
}}

Code:Misc

The following lines should be added to your configuration file:
$sexy = yes;
$intelligent = yes;


Ο κώδικας για το παραπάνω κουτάκι κώδικα έχει ως εξής:

{{Code:Misc
|The following lines should be added to your configuration file:
|<pre>$sexy = yes;
$intelligent = yes;</pre>
}}

Code:Misc2

The following lines should be added to your configuration file:
$sexy = yes;
$intelligent = yes;


Ο κώδικας για το παραπάνω κουτάκι κώδικα έχει ως εξής:

{{Code:Misc2
|The following lines should be added to your configuration file:
|<pre>$sexy = yes;
$intelligent = yes;</pre>
}}

Code:Transclude

This can be used to create a transcluded, titled page, like so