📄 urlfilter.jc
字号:
/*
* urlfilter.jc
*
* A demonstration script for the JewelScript language.
* Filters all link locations from all input html files and
* writes them into output html files.
*
* usage: jilrun urlfilter.jc <file1.html> <file2.html> ...
*
* There should be a file "test.html" in the same folder than
* this script that you can use to test this script.
*/
import stdlib;
import File;
import Trex;
/* constants */
const long kRead = 0;
const long kWrite = 1;
/* cout */
function cout(const var& v) { stdlib::Print( stdlib::ToString(v) ); }
/* main */
function string main(const array& args)
{
cout({ "urlfilter: ", args.length - 1, " file(s) specified\n" });
for( long i = 1; i < args.length; i++ )
{
// create input file object
File srcFile = new File( args[i], kRead );
// create output file object from input filespec
File dstFile = new File();
dstFile.path = srcFile.path;
dstFile.name = srcFile.name + "_filtered";
dstFile.type = "html";
dstFile.mode = kWrite;
// process the file
if( Process(srcFile, dstFile) )
break;
}
return "Done.";
}
/* Process */
function long Process(File& srcFile, File& dstFile)
{
// check source file is html
if( srcFile.type != "htm" && srcFile.type != "html" )
{
stdlib::Printf("Error: %s is not a html file.\n", srcFile.fileSpec);
return -1;
}
// check source file exists
if( !srcFile.Exists() )
{
stdlib::Printf("Error: %s could not be found.\n", srcFile.fileSpec);
return -1;
}
// load source html file
cout({ "urlfilter: Loading file ", srcFile.fileSpec, "\n" });
string srcText = "";
if( srcFile.Open() )
{
stdlib::Printf("Error: %s could not be opened for reading.\n", srcFile.fileSpec);
}
else
{
srcFile.ReadText( srcText );
srcFile.Close();
}
// get a list of urls from source file; we use a regular expression for this
cout( "urlfilter: Getting a list of urls from source file...\n" );
Trex regex = /"(HREF|href)=['"]?(HTTP|http)://([a-zA-Z0-9/&=_\?\-\.]+)"/;
array& urlList = regex.Search( srcText, /"<A HREF="http://$3">$3</A><BR>"/ "\n" );
// create destination file from URL list
cout( "urlfilter: Creating destination file contents...\n" );
string dstText = "<HTML>\n<HEAD>\n</HEAD>\n<BODY>\n";
for( long i = 0; i < urlList.length; i++ )
dstText += urlList[i];
dstText += "</BODY>\n</HTML>\n";
// save destination html file
cout({ "urlfilter: Writing file ", dstFile.fileSpec, "\n" });
if( dstFile.Open() )
{
stdlib::Printf("Error: %s could not be opened for writing.\n", dstFile.fileSpec);
}
else
{
dstFile.WriteText( dstText );
dstFile.Close();
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -