<commit_before>
'use strict';

var path = require('path');

var helpers = require('../helpers');

var expression = /(?:\[[^\]]*\])?\{@(?:link|glossary)\ *(?:(?!http)[^#~\s]*)(?:[#~](?:\S*))?\s*\}/g,
	extract = /(?:\[([^\]]*)\])?\{@(?:link|glossary)\ *([^#~\s]*)(?:[#~](\S*))?\s*\}/;

exports.process = function (doclet) {
	
	renderLinks(doclet);
};

function findLinks (text) {
	return text.match(expression);
}

function buildHref (elem) {
	var db = helpers.db,
		doclet = db({longname: elem}).first(),
		href,
		kind;
	
	if (!doclet) return false;
	
	kind = doclet.kind;
	
	// if the kind is class we use the word 'kind' instead arbitrarily because, well, this is
	// for enyo after all
	if (kind == 'class') kind = 'kind';
	
	href = '#/' + kind + '/' + elem;
	helpers.addLinkFor(elem, href);
	return href;
}

function renderLinks (doclet) {
	
	var text = doclet.content,
		links;
	
	// for now we only post-process for links against the greater rendered content regions -
	// lesser elements don't get rendered individually but their content will have been rendered
	// in the outer one so we wait till then to grab them all
	if (text) {
		links = findLinks(text);
		
		if (links) {
			links.forEach(function (ln) {
				var tok = extract.exec(ln),
			
					// if requested, anything in the [] before the {@link} tag
					linkText = tok[1],
				
					// the actual root thing it was targeting
					elem = tok[2],
				
					// anything after # if it existed
					target = tok[3],
					href,
					str = '<a href="';
					
				if (ln.indexOf('glossary') > -1) {
					target = elem;
					href = '#/glossary';
					linkText = linkText || target;
				} else {
				
					// if there was no explicit link text requested the default will be used
					if (!linkText) linkText = elem;
					href = helpers.linkFor(elem);

					// if we haven't encountered the root element before we have to create it
					if (!href) {

						// note this automatically registers it for faster lookup if requested again
						href = buildHref(elem);
						if (href === false) {
						
							helpers.error(
								__filename.replace(process.cwd(), '') + ':renderLinks() ->\n\tIn ' + doclet.source + ' near line ' + doclet.sourceLine +
								', link cannot be resolved `' + ln + '`'
							);
						
							// in this case there was an error and, while we will produce the error to
							// console we will also make sure it can't be missed when scanning the
							// output for a better chance at being caught!
							linkText = 'ERROR FINDING REFERENCE IN LINK FOR "' + elem + '"';
							href = 'javascript:void(0);';
						}
					}
				}

				str += href;

				// if there is a target we append that too
				if (target) str += (':' + target);

				str += ('">' + linkText + '</a>');

				text = text.replace(ln, str);
			});
			
			doclet.content = text;
		}
	}
	
}<commit_msg>interim commit reworking link resolution - separating out target types of links<commit_after>
'use strict';

var path = require('path');

var helpers = require('../helpers');

exports.process = function (doclet) {
	if (doclet.content) resolveLinks(doclet);
};

function resolveLinks (doclet) {
	renderGlossary(doclet);
	renderLinkPlain(doclet);
	renderLinks(doclet);
}

function renderGlossary (doclet) {
	var expression = /(?:\[[^\]]*\])?\{@glossary\ *\S*\}/g,
		extract = /(?:\[([^\]]*)\])?\{@glossary\ *(\S*)\}/,
		text = doclet.content,
		terms = text.match(expression);
	
	if (terms) {
		terms.forEach(function (term) {
			var tok = extract.exec(term);
			text = text.replace(term, '<a class="glossary" href="#/glossary:' + tok[2] + '">' + (tok[1] || tok[2]) + '</a>')
		});
		
		doclet.content = text;
	}
}

function renderLinkPlain (doclet) {
	var expression = /(?:\[[^\]]*\])?(?:\{@linkplain\ *(?:[^\}]*)\})/g,
		extract = /(?:\[([^\]]*)\])?(?:\{@linkplain\ *([^\}]*)\})/,
		text = doclet.content,
		links = text.match(expression);
	
	if (links) {
		links.forEach(function (link) {
			var tok = extract.exec(link),
				href = tok[2],
				linkText = tok[1],
				parts;
			
			if (!linkText) {
				// there is a possibility they used the format after the href so check that now
				parts = href.split(' ');
				if (parts.length > 1) {
					href = parts.shift();
					linkText = parts.join(' ');
				} else {
					linkText = href;
				}
			}
			
			text = text.replace(link, '<a class="linkplain" href="' + href + '">' + linkText + '</a>');
		});
		
		doclet.content = text;
	}
}

function renderLinks (doclet) {

	var expression = /(?:\[[^\]]*\])?\{@link\ *(?:[^\}]*)\}/g,
		extract = /(?:\[([^\]]*)\])?\{@link\ *(?:([^#~\s]*)(?:[#~](\S*))?)(.*)\}/;

	var text = doclet.content,
		links = text.match(expression);

	if (links) {
		links.forEach(function (ln) {
			var tok = extract.exec(ln),

				// if requested, anything in the [] before the {@link} tag
				linkText = tok[1],

				// the actual root thing it was targeting
				elem = tok[2],

				// anything after # if it existed
				target = tok[3],
				href,
				str = '<a href="';

			// if there was no explicit link text requested the default will be used
			if (!linkText) linkText = tok[4] || elem;
			href = helpers.linkFor(elem);

			// if we haven't encountered the root element before we have to create it
			if (!href) {

				// note this automatically registers it for faster lookup if requested again
				href = buildHref(elem);
				if (href === false) {

					helpers.error(
						__filename.replace(process.cwd(), '') + ':renderLinks() ->\n\tIn ' +
						doclet.source + ' near line ' + doclet.sourceLine +
						', link cannot be resolved `' + ln + '`'
					);

					// in this case there was an error and, while we will produce the error
					// to console we will also make sure it can't be missed when scanning
					// the output for a better chance at being caught!
					linkText = 'ERROR FINDING REFERENCE IN LINK FOR "' + elem + '"';
					href = 'javascript:void(0);';
				}
			}
			

			str += href;
			// if there is a target we append that too
			if (target) str += (':' + target);
			str += ('">' + linkText + '</a>');
			text = text.replace(ln, str);
		});

		doclet.content = text;
	}

}


function buildHref (elem) {
	var db = helpers.db,
		doclet = db({longname: elem}).first(),
		href,
		kind;
	
	if (!doclet) return false;
	
	kind = doclet.kind;
	
	// if the kind is class we use the word 'kind' instead arbitrarily because, well, this is
	// for enyo after all
	if (kind == 'class') kind = 'kind';
	
	href = '#/' + kind + '/' + elem;
	helpers.addLinkFor(elem, href);
	return href;
}
{'fraction_non_alphanumeric': 0.1818460125516608, 'fraction_numerical': 0.002449104546150314, 'mean_word_length': 2.6936122102882987, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2335950', 'n_tokens_mistral': 2625, 'n_tokens_neox': 2142, 'n_words': 669}