URL Parser and Builder documentation

netteman
Member | 125
+
0
-

Hi,

the documentation says that “Class UrlScript is a descendant of Url” https://doc.nette.org/en/http/urls#…

I thinks it was true for 2.4 https://api.nette.org/…ipt.php.html#…

but not for 3.0 https://api.nette.org/…lScript.html

Btw, Can I see the script code in api for version 3.0 just like in 2.4 ?

David Grudl
Nette Core | 8227
+
0
-

If you find out how to configure Sami (used for 3.0 API) to show the code, I'll add it.

netteman
Member | 125
+
0
-

I'm afraid I'm not skilled enough to do this but how about something less sophisticated?

Sami knows some line numbers so I think that adding just links to github could be good a starting point ;-)

https://github.com/…rlScript.php#L69

David Grudl
Nette Core | 8227
+
0
-

Ok, do it and send me it.

netteman
Member | 125
+
+1
-

I don't know how but here's my JavaScript/jQuery code I inject into the page using an addon and it almost always works ;-)

<script>

var path = [];
var gitUrl = '';

function setPath(){

	$( ".breadcrumb li" ).each(function() {

		switch($(this).text()) {
		  	case 'class':
		  	case 'interface':
			case '\\':
				break;

			default:
				path.push($(this).text());
		}

	});

}//end func


function setGitUrl(){

	var beforeBlob = [];
	var afterSrc = [];


	for(var i = 0; i < path.length; i++){

		switch(path[0]){

			case 'Nette':

				if(i < 2){
					beforeBlob.push(path[i]);
				}

				if(i > 0){
					afterSrc.push(path[i]);
				}

				break;


			case 'Tracy':

				if(i == 0){
					beforeBlob.push('Nette');
				}

				if(i < 1){
					beforeBlob.push(path[i]);
				}


				afterSrc.push(path[i]);


				if(i == path.length - 1){
					afterSrc.push(path[i]);
				}

				break;


			case 'NetteModule':

				if(i == 0){
					beforeBlob.push('Nette');
					beforeBlob.push('Application');
					afterSrc.push('Application');
				}

				if(i > 0){
					afterSrc.push(path[i]);
				}

				break;


			default:

				if(i == 0){
					beforeBlob.push('Nette');
					beforeBlob.push(path[i]);
				}


				afterSrc.push(path[i]);


		}//end switch


	}//end for

	gitUrl = 'https://github.com/'+beforeBlob.join('/').toLowerCase()+'/blob/master/src/'+afterSrc.join('/')+'.php';

}//end func


function appendHtml(){

	$('div.location').each(function(){

		var text = $(this).text();

		var line = text.replace(/\D/g, '');

		$(this).append(' <a target="_blank" href="'+gitUrl+'#L'+line+'">Git</a>');

	});

}//end func


setPath();
setGitUrl();
appendHtml();

</script>

Last edited by netteman (2020-05-19 15:50)

David Grudl
Nette Core | 8227
+
+3
-

Tak je to tam :)

netteman
Member | 125
+
0
-

Wow, that was quick! I'll try to update the code to make it better. I know it doesn't work for example with PhpGenerator – the code generates:

//github.com/nette/phpgenerator/blob/master/src/PhpGenerator/Factory.php#L22

but you need

//github.com/nette/php-generator/blob/master/src/PhpGenerator/Factory.php#L22

netteman
Member | 125
+
0
-

This should take care of the non-working github urls.

<script>

var path = [];
var gitUrl = '';

function setPath(){

	$( ".breadcrumb li" ).each(function( index ) {

		switch($(this).text()) {
		  	case 'class':
		  	case 'interface':
		  	case 'trait':
			case '\\':
				break;

			default:
				path.push($(this).text());
		}

	});

}//end fce



function formatBeforeBlob(array){

	let str = '';

	str = array.join('/').toLowerCase();

	str = str.replace('phpgenerator','php-generator');
	str = str.replace('componentmodel','component-model');
	str = str.replace('iterators','utils');

	return str;

}//end func



function formatAfterSrc(array){

	let str = '';

	str = array.join('/');

	str = str.replace('Tracy/ILogger/','Tracy/Logger/');
	str = str.replace('Tracy/IBarPanel/','Tracy/Bar/');
	str = str.replace('Tracy/Helpers/', 'Tracy/');
	str = str.replace('Tracy/FireLogger/', 'Tracy/Logger/');
	str = str.replace('Tracy/DefaultBarPanel/', 'Tracy/Bar/');
	str = str.replace('Nette/Schema', 'Schema');
	str = str.replace('DI/ServiceCreationException','DI/exceptions');
	str = str.replace('Application/BadRequestException','Application/exceptions');
	str = str.replace('Caching/IBulkReader','Caching/BulkReader');
	str = str.replace('Caching/IStorage','Caching/Storage');
	str = str.replace('Latte/Compiler','Latte/Compiler/Compiler');
	str = str.replace('Latte/HtmlNode','Latte/Compiler/HtmlNode');
	str = str.replace('Latte/MacroNode','Latte/Compiler/MacroNode');
	str = str.replace('Latte/MacroTokens','Latte/Compiler/MacroTokens');
	str = str.replace('Latte/Parser','Latte/Compiler/Parser');
	str = str.replace('Latte/PhpHelpers','Latte/Compiler/PhpHelpers');
	str = str.replace('Latte/PhpWriter','Latte/Compiler/PhpWriter');
	str = str.replace('Latte/Token','Latte/Compiler/Token');
	str = str.replace('Latte/TokenIterator','Latte/Compiler/TokenIterator');
	str = str.replace('Latte/Tokenizer','Latte/Compiler/Tokenizer');
	str = str.replace('Latte/CompileException', 'Latte/exceptions');
	str = str.replace('Latte/RegexpException', 'Latte/exceptions');


	return str;

}//end func



function formatGitUrl(url){

	let str = url;

	str = str.replace('nette/staticclass/blob/master/src/StaticClass.php', 'nette/utils/blob/master/src/Utils/StaticClass.php');

	str = str.replace('nette/smartobject/blob/master/src/SmartObject.php', 'nette/utils/blob/master/src/Utils/SmartObject.php');

	str = str.replace('nette/configurator/blob/master/src/Configurator.php','nette/bootstrap/blob/master/src/Bootstrap/Configurator.php');

	str = str.replace('nette/localization/blob/master/src/Localization/ITranslator', 'nette/utils/blob/master/src/Utils/ITranslator');

	str = str.replace('nette/utils/blob/master/src/Utils/Finder.php', 'nette/finder/blob/master/src/Utils/Finder.php');

	str = str.replace('nette/utils/blob/master/src/Utils/SafeStream.php', 'nette/safe-stream/blob/master/src/SafeStream/SafeStream.php');

	str = str.replace('nette/loaders/blob/master/src/Loaders/RobotLoader.php', 'nette/robot-loader/blob/master/src/RobotLoader/RobotLoader.php');

	return str;

}//end func




function setGitUrl(){

	let beforeBlob = [];
	let afterSrc = [];


	for(let i = 0; i < path.length; i++){

		switch(path[0]){

			case 'Nette':

				if(i < 2){
					beforeBlob.push(path[i]);
				}

				if(i > 0){
					afterSrc.push(path[i]);
				}

				break;


			case 'Tracy':

				if(i == 0){
					beforeBlob.push('Nette');
				}

				if(i < 1){
					beforeBlob.push(path[i]);
				}


				afterSrc.push(path[i]);


				if(i == path.length - 1){
					afterSrc.push(path[i]);
				}

				break;


			case 'NetteModule':

				if(i == 0){
					beforeBlob.push('Nette');
					beforeBlob.push('Application');
					afterSrc.push('Application');
				}

				if(i > 0){
					afterSrc.push(path[i]);
				}


				break;


			default:

				if(i == 0){
					beforeBlob.push('Nette');
					beforeBlob.push(path[i]);
				}


				afterSrc.push(path[i]);


		}//end switch


	}//end for

	let url = 'https://github.com/'+formatBeforeBlob(beforeBlob)+'/blob/master/src/'+formatAfterSrc(afterSrc)+'.php';

	gitUrl = formatGitUrl(url);

}//end fce



function appendHtml()
{
	$('div.location').each(function(){
		let text = $(this).text();
		let line = text.replace(/\D/g, '');
		$(this).wrap('<a target="_blank" href="'+gitUrl+'#L'+line+'"></a>');
	});
}


$(function() {
	setPath();
	setGitUrl();
	appendHtml();
});

</script>

Last edited by netteman (2020-05-20 15:23)

David Grudl
Nette Core | 8227
+
+1
-

Thanks, updated.

netteman
Member | 125
+
0
-

I found an error in formatGitUrl()

The problem:
str = str.replace(‘nette/smartobject/blob/master/src/StaticClass.php’, ‘nette/utils/blob/master/src/Utils/SmartObject.php’);

This code works (fixed in the previous post):
str = str.replace(‘nette/smartobject/blob/master/src/SmartObject.php’, ‘nette/utils/blob/master/src/Utils/SmartObject.php’);