Textantrieb | UText/1 | UText/1.2 Manual
FILE.pm
This class supports writing into a text file or modifying it.
Writing
To write into a file:
$OUT=new FILE $outfile;
fprint $OUT '<table class="infobox">';
The function fprintln($cnt);
does the same as fprint($cnt."\n");
.
Writing strings
After creating an instance of FILE you can bind it to a UText object:
$ut = new UText;
...
$fh = new FILE(...);
$fh -> bind($ut);
After that, you can write into the file with:
fout $fh 'sample at page [v title]';
This does the same as:
my $out = $ut->out('sample at page [v title]');
$fh -> fprint($out);
Writing HTML Heading Lines
The module can optionally process HTML header lines.
If HTML preprocessing is active, wenn feeding lines with fprint
or fprintln
, HTML head lines embedded between <head>
and </head>
are recognised and processed, so that the resulting saved file is well-formed HTML (there is only one <head>
section at the top of the file) and no single head line gets repeated.
This way one can add a heading line when outputing the html document body, which is useful for example when inserting some code that requires a specific heading line.
One can activate this functionality setting the property processheader
:
$fh->{processheader}=1;
Character shortcuts
Because square brackets are always interpreted as tags and blank lines are removed (see ”cleaning“ below) there are these shortcuts to write them in files: < followed by ° becomes [, ° followed by > becomes ], and < followed by ° and > becomes a blank line.
Saving
To save changes: commit $OUT;
. This only saves to disk if the file contents were changed.
To save always: commit $OUT 1;
same as above but it touches the file if it was unchanged.
A call to commit $OUT 1, $mtime;
sets the atime and mtime of an unchanged file to the given timestamp instead of now.
To quit without saving: cancel $OUT;
Before saving a file, it is by default ”cleaned up“: all empty lines are removed. This is to kept HTML pages cleaner. This behavior can be avoided by setting the property ”cleanup“ of the FILE object to false before commiting: $fh->{cleanup}=0;
.
Changing
to update file contents ($page is relative to $home):
FILE::sethome($home);
FILE::change($page,$search_regexp,$replace_regexp);
to update file contents between tags:
FILE::sethome($home);
FILE::edit('index.html','NOTES',$notes);
this replaces (multiline) contents of file $home/index.html between tags:
<!-- NOTES BEGIN --> ...contents... <!-- NOTES END -->
Summary
To get a summary of the updated files on the last session. In a UText script:
print %summary
The same in a Perl script:
print FILE::get_summary();
This prints out a line such as ”14 files made in 11 s: one file created, 4 files updated.“
To reset the summary counters use clear summary
in a UText script and FILE::clear_summary();
in Perl.