Forum Moderators: coopster

Message Too Old, No Replies

automatic linebreak (<li>)

         

jman11

3:46 am on Oct 29, 2009 (gmt 0)

10+ Year Member



hi, i see these <li> tags for when people display code all the time, but surely they dont go in for each line and insert a <li></li>. is there a way to display code as numbered lines like in this picture?

<snip>

[edited by: dreamcatcher at 7:06 pm (utc) on Oct. 29, 2009]
[edit reason] No Urls Please. See TOS. [/edit]

lavazza

4:26 am on Oct 29, 2009 (gmt 0)

10+ Year Member



The <li> tag is used for controlling the OUTPUT... not the SOURCE CODE, which is what is (was*) seen following the link you posted

Many (so called) WYSIWYG and Text Code Editors [webmasterworld.com] support line numbering... and stuff like syntax highlighting and folding - all of which make it easier to 'jump' to the relevant section if... I mean when validators find bugs in your html, css, php, javascript, etc

___________
* was: your link will be removed as it contravenes the ToS here at WebmasterWorld

rocknbil

5:51 pm on Oct 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More info required. :-)

A sample idea, let's say you have your code in a file.


<table>
<tr><td><label for="Email">Email"</label></td><input type="text" name="Email" id="Email"></td></tr>
<!-- etc -->
</table>

You could open the file, and read it line by line:


list = '';
$fhandle = @fopen($myfile, "r");
if (is_file($myfile) and $fhandle) {
while (!feof($fhandle)) {
$buffer = fgets($fhandle, 4096);
$buffer = htmlentities($buffer);
// so the code doesn't render
$list .= '<li>' . $buffer . '</li>';
}
fclose($fhandle);
if ($list != '') {
$list = '<ol>' . $list . '</ol>';
echo $list; // or return, in a function
}
else { echo "No sample code." }
}
else { echo "OOPS. Invalid file." }

How you'd insert spacing to emulate the tabs for the code formatting will be a task you'll have to figure out.

jman11

6:18 pm on Oct 29, 2009 (gmt 0)

10+ Year Member



well @ rocknbil wouldnt that read the whole page worth of contents? im just looking at like writing a tutorial and for the the <code> tags just have <li> for each line thats numbered.

rocknbil

7:52 pm on Oct 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Baby steps to completing the task. :-) I used a file as an example, all you'd have in that file is the sample code, not an entire HTML page. You can extract it from a database, hard code it into a static page, however you want to access the samples.

lavazza

9:47 pm on Oct 31, 2009 (gmt 0)

10+ Year Member



If you use
  • the SciTE text editor with line numbers turned on
    AND
  • 'print' using CutePDF (a 'virtual printer')
    the line numbers are preserved
    :)
  •