[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[sc-dev] Auto link fixer
this is not really done yet, but I just used it automatically fix the
links in all of the GUI help files in about 10 minutes.
help isn't working yet, you still have to reexecute the code averter
every use.
try loading 10 help files, set verbosity to "NO PAUSES", and watch it
rattle through all the selected files adn fix the links.
In more verbose levels, you have to hit resume a lot. In the default
leverl, you only hit resume twice, and the finished help file opens for
inspeciton afterwards.
have a happy ...
jost
(
// on all the verbose levels, remember to hei resume !
{arg path="** Replace String or use Get Path **";
var win,startButton,button2,menu1, r, label,task,pathLabel,countLabel,currentLink, userLink, verbose=1, states;
var fileFixLinks ,searchPath, skipLink=false,skipButton,func,currentPath;
path.isString.if{path=[path]};
w=Window.new("Link Fixer", Rect(100,Window.screenBounds.height-500,400,400)).front.alwaysOnTop_(true);
w.addFlowLayout;
StaticText(w,170@20).string_("Path:");
Button(w, 70 @20).states_([["Path Match"]])
.action_({searchPath.string=searchPath.string.dirname++"/*";searchPath.doAction});
Button(w, 70 @20).states_([["Get Path", Color.black, Color. green(0.6)]])
.action_({
Dialog.getPaths({|p| (p.size>1).if
{searchPath.string="***** Multiple Files ******";
path=p.collect{|p| p.asRelativePath}.postln}
{searchPath.string=p[0].asRelativePath;
searchPath.doAction};}, {}, 100)});
Button(w, 70 @20).states_([["Help"]])
.action_({});
searchPath=TextField(w,390@30).string_(0.asString)
.boxColor_(Color.grey.alpha_(0.3))
.stringColor_(Color.white)
.string_(path[0]).action_{path=[searchPath.string].postln};
countLabel=StaticText(w,100@20).string_(" count: "++ 0.asString).background_(Color.grey);
menu1=EZPopUpMenu(w, 286@20,"Verbosity:",
['file results Only'->{verbose=1}, 'link not found only'->{verbose=2},
'confirm all replaces'->{verbose=3},'-'->{},'**NO PAUSES**'->{verbose=0}]);
StaticText(w,390@20).string_("Current File:");
currentPath=StaticText(w,390@20).string_("").background_(Color.grey);
StaticText(w,390@20).string_("Current Link:");
currentLink=StaticText(w,390@20).string_("").background_(Color.grey);
StaticText(w,390@20).string_("Replacement Link:");
userLink=TextField(w,390@30).string_("").boxColor_(Color.grey.alpha_(0.3));
2.do{w.view.decorator.nextLine};
skipButton=Button(w, 392 @30).states_([["Skip Link"]])
.action_({skipLink=true; userLink.string=""; startButton.valueAction_(1); skipButton.enabled=false})
.enabled_(false);
2.do{w.view.decorator.nextLine};
states=[["Start",Color.black, Color.green(0.7)],["Stop/Pause",Color.black, Color.red(0.7)]];
startButton=Button(w, w.view.decorator.indentedRemaining)
.font_(GUI.font.new("Helvetica",24))
.states_(states)
.action_({arg b; [
{task.pause},
{states[0].put(0,"Resume When Ready");
b.states=states;
skipButton.enabled_(false);
skipLink=false;
task.play(AppClock);
}].at(b.value).value});
task = Task{
//"Path: %".format(path).postln;
path.do{arg path;
//path.pathMatch.postln;
path.pathMatch.do{arg path;
func={ arg path="Help/GUI/Main-GUI/*", verbose;
var string, f, reg, result=[],replacements=[], outString="",k;
if( { path.basename.splitext[1]=="html"}.try ){
currentPath.string=path.postln;
(verbose>0).if{
startButton.value_(0);
0.1.wait;
task.pause;
};
0.2.wait;
//path.postln;
try{
f=File.new(path,"r");
string = f.readAllString;
f.close;
// this parses all found links into [[position, completeLink1],[position, url1], ...]
reg="<a href=[\"]([^\"]*)[\"]>[a-zA-Z0-9_< >\"/=\-]*</a>";
result=string.findRegexp(reg);
(result.size/2).do{arg i ;
var link, oldPath,newPath,oldPath2,pathStart, pathEnd;
link = result[i*2][1];
#pathStart,oldPath= result[(i*2)+1];
pathEnd = (pathStart + oldPath.size);
// get rid of the url specifier
oldPath2=oldPath.replace("http:","");
oldPath2=oldPath2.replace("file:","");
oldPath2=oldPath2.replace("SC:","");
oldPath2=oldPath2.replace("file:","");
newPath={oldPath2.basename.splitext[0]
.findHelpFile.asRelativePath(path.dirname)}.try;
if (oldPath!=newPath){userLink.boxColor_(Color.red.alpha_(0.3))}
{userLink.boxColor_(Color.grey.alpha_(0.3))};
userLink.string=newPath;
currentLink.string=oldPath;
if( newPath.isNil ||(verbose>2 &&(newPath != oldPath))){
newPath.isNil.if{
userLink.string=("***Link target not not found *** ").post;
oldPath.postln};
skipButton.enabled=true;
startButton.value=0;
task.pause;
0.2.wait;
skipLink.if{
"** Skipped: %".format(link).postln;
newPath=nil;
}{
newPath=userLink.string;
//newPath.postln;
};
};
if (oldPath!=newPath){ //newPath.postln;
replacements=replacements ++[[newPath,pathStart,oldPath.size]];
link.postln;
oldPath.postln;
newPath.postln;
"".postln;
};
countLabel.string=(i+1).asString;
//(i+1).postln;
//0.01.wait;
};
"replacements collected".postln;
//replacements.postln;
k=0;
(replacements.size>0).if{
"starting replacements".postln;
0.1.wait;
{
var rStart=string.size, rEnd=1.neg,rrEnd=1.neg, rString;
string.do{arg str, i;
((i%300)==0).if{".".postln};
if ((i==replacements[k][1]))
{ "Replacement: ".format(k).postln;
rString = replacements[k][0].postln;
rStart = replacements[k][1];
rrEnd = (rStart+replacements[k][2]);
rEnd = (rStart + rString.size);
if (k < (replacements.size-1)) {k=k+1};
0.005.wait;
};
if((i>=rStart) && (i<rrEnd)){}// remove old string
{ if (i==(rrEnd)) { outString=outString++rString.asString};
outString = outString ++ str };
//(str==nil).postln;
//outString = outString ++ str
//0.005.wait;
};
0.1.wait;
}.value;
"finished replacements".postln;
0.1.wait;
"writing file:/ %".format(path).postln;
// outString.postln
f=File.new(path,"w");
f.write(outString);
f.close;
(verbose>0).if{Document.open(path)};
};
(replacements.size.asString ++ " links replaced").postln;
"\n****finished******".postln;
}{"Could not open Document : "++path};
};
}.value(path,verbose);
1.wait;
};
};
startButton.enabled=false;
};
}.value;
)