<commit_before>/*

© Copyright 2016 <NAME>

This file is part of strugee.net.

strugee.net is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

strugee.net is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with strugee.net.  If not, see <http://www.gnu.org/licenses/>.

*/

// Polyfills

if (!window.Promise) {
	require('es6-promise').polyfill(); // eslint-disable-line global-require
}
require('whatwg-fetch');

(function() {
	'use strict';

	function strip(html) {
		// https://stackoverflow.com/a/822486/1198896
		var div = document.createElement('div');
		div.innerHTML = html;
		return div.textContent || div.innerText || '';
	}

	// Kick off the fetch ASAP because it doesn't require the DOM and so we don't want to wait for that
	var req = fetch('https://webmention.io/api/mentions?target=' + window.location);

	var initialized = false;

	function init() {
		if (initialized) return;
		initialized = true;

		var mentionsElement = document.getElementById('webmentions');

		// Only when the DOM is ready do we register Promise stuff that interacts with it
		req.then(function(response) {
			return response.json();
		})
		.then(function(data) {
			if (data.links.length === 0) {
				// No replies, so we bail
				var noReplies = document.createElement('p');
				noReplies.appendChild(document.createTextNode('No replies - yet! Maybe you\'d like to be the first?'));
				mentionsElement.appendChild(noReplies);

				var webmentionDocs = document.createElement('p');
				var webmentionLink = document.createElement('a');
				webmentionDocs.appendChild(document.createTextNode('See '));
				webmentionLink.appendChild(document.createTextNode('webmention.net'));
				webmentionLink.href = 'https://webmention.net';
				webmentionDocs.appendChild(webmentionLink);
				webmentionDocs.appendChild(document.createTextNode(' for the docs.'));
				mentionsElement.appendChild(webmentionDocs);

				return;
			}

			data.links.forEach(function(link) {
				// TODO handle this, maybe?
				if (link.private) return;

				// TODO this probably doesn't handle all types? But I couldn't find sufficient documentation

				// TODO look at what "verified" actually means
				if (!link.verified) return;

				var p = document.createElement('p');

				// Avatar
				// TODO do something better with missing avatars
				if (link.data.author.photo) {
					var avatarLink = document.createElement('a');
					avatarLink.setAttribute('href', link.data.author.url);
					var avatar = document.createElement('img');
					avatar.classList.add('webmention-avatar');
					avatar.setAttribute('src', link.data.author.photo);
					avatar.setAttribute('alt', link.data.author.name + '\'s avatar');
					avatarLink.appendChild(avatar);
					p.appendChild(avatarLink);
				}

				p.appendChild(document.createTextNode(' '));

				// "<NAME>"...
				var author = document.createElement('a');
				author.setAttribute('href', link.data.author.url);
				author.appendChild(document.createTextNode(link.data.author.name));
				p.appendChild(author);

				var shortContent = strip(link.data.content);
				if (shortContent.length > 60) {
					// TODO slice on word boundaries
					shortContent = shortContent.slice(0, 60) + '…';
				}

				switch (link.activity.type) {
				case 'reply':
					// ..."replied with"...

					p.appendChild(document.createTextNode(' replied with '));

					// ..."check out this article! http://example.com"
					var source = document.createElement('a');
					source.setAttribute('href', link.data.url);
					source.appendChild(document.createTextNode(shortContent));
					p.appendChild(source);
					break;
				case 'link':
					// ..."linked to this and added "...

					p.appendChild(document.createTextNode(' linked to this and added '));

					// ..."check out this article! http://example.com"
					var source = document.createElement('a');
					source.setAttribute('href', link.data.url);
					source.appendChild(document.createTextNode(shortContent));
					p.appendChild(source);
					break;
				case 'like':
					// ..."liked this"
					p.appendChild(document.createTextNode(' '));
					var likeSource = document.createElement('a');
					likeSource.appendChild(document.createTextNode('liked this'));
					likeSource.setAttribute('href', link.data.url);
					p.appendChild(likeSource);
					break;
				default:
					// ..."sent a "...
					p.appendChild(document.createTextNode(' sent a '));
					// ..."Webmention notification"
					var mentionSource = document.createElement('a');
					mentionSource.appendChild(document.createTextNode('Webmention notification'));
					mentionSource.setAttribute('href', link.data.url);
					p.appendChild(mentionSource);
					break;
				}

				mentionsElement.appendChild(p);
			});
		})
		.catch(function(err) {
			console.log('Encountered Webmention error:', err);
			mentionsElement.appendChild(document.createTextNode('Couldn\'t fetch Webmentions. Try refreshing?'));
		});
	}

	document.addEventListener('DOMContentLoaded', init, false);
	var oldListener = document.onreadystatechange;<commit_msg>Display bookmark Webmentions<commit_after>/*

© Copyright 2016 <NAME>

This file is part of strugee.net.

strugee.net is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

strugee.net is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with strugee.net.  If not, see <http://www.gnu.org/licenses/>.

*/

// Polyfills

if (!window.Promise) {
	require('es6-promise').polyfill(); // eslint-disable-line global-require
}
require('whatwg-fetch');

(function() {
	'use strict';

	function strip(html) {
		// https://stackoverflow.com/a/822486/1198896
		var div = document.createElement('div');
		div.innerHTML = html;
		return div.textContent || div.innerText || '';
	}

	// Kick off the fetch ASAP because it doesn't require the DOM and so we don't want to wait for that
	var req = fetch('https://webmention.io/api/mentions?target=' + window.location);

	var initialized = false;

	function init() {
		if (initialized) return;
		initialized = true;

		var mentionsElement = document.getElementById('webmentions');

		// Only when the DOM is ready do we register Promise stuff that interacts with it
		req.then(function(response) {
			return response.json();
		})
		.then(function(data) {
			if (data.links.length === 0) {
				// No replies, so we bail
				var noReplies = document.createElement('p');
				noReplies.appendChild(document.createTextNode('No replies - yet! Maybe you\'d like to be the first?'));
				mentionsElement.appendChild(noReplies);

				var webmentionDocs = document.createElement('p');
				var webmentionLink = document.createElement('a');
				webmentionDocs.appendChild(document.createTextNode('See '));
				webmentionLink.appendChild(document.createTextNode('webmention.net'));
				webmentionLink.href = 'https://webmention.net';
				webmentionDocs.appendChild(webmentionLink);
				webmentionDocs.appendChild(document.createTextNode(' for the docs.'));
				mentionsElement.appendChild(webmentionDocs);

				return;
			}

			data.links.forEach(function(link) {
				// TODO handle this, maybe?
				if (link.private) return;

				// TODO this probably doesn't handle all types? But I couldn't find sufficient documentation

				// TODO look at what "verified" actually means
				if (!link.verified) return;

				var p = document.createElement('p');

				// Avatar
				// TODO do something better with missing avatars
				if (link.data.author.photo) {
					var avatarLink = document.createElement('a');
					avatarLink.setAttribute('href', link.data.author.url);
					var avatar = document.createElement('img');
					avatar.classList.add('webmention-avatar');
					avatar.setAttribute('src', link.data.author.photo);
					avatar.setAttribute('alt', link.data.author.name + '\'s avatar');
					avatarLink.appendChild(avatar);
					p.appendChild(avatarLink);
				}

				p.appendChild(document.createTextNode(' '));

				// "<NAME>"...
				var author = document.createElement('a');
				author.setAttribute('href', link.data.author.url);
				author.appendChild(document.createTextNode(link.data.author.name));
				p.appendChild(author);

				var shortContent = strip(link.data.content);
				if (shortContent.length > 60) {
					// TODO slice on word boundaries
					shortContent = shortContent.slice(0, 60) + '…';
				}

				// TODO DRY this up
				switch (link.activity.type) {
				case 'reply':
					// ..."replied with"...

					p.appendChild(document.createTextNode(' replied with '));

					// ..."check out this article! http://example.com"
					var source = document.createElement('a');
					source.setAttribute('href', link.data.url);
					source.appendChild(document.createTextNode(shortContent));
					p.appendChild(source);
					break;
				case 'link':
					// ..."linked to this and added "...

					p.appendChild(document.createTextNode(' linked to this and added '));

					// ..."check out this article! http://example.com"
					var source = document.createElement('a');
					source.setAttribute('href', link.data.url);
					source.appendChild(document.createTextNode(shortContent));
					p.appendChild(source);
					break;
				case 'like':
					// ..."liked this"
					p.appendChild(document.createTextNode(' '));
					var likeSource = document.createElement('a');
					likeSource.appendChild(document.createTextNode('liked this'));
					likeSource.setAttribute('href', link.data.url);
					p.appendChild(likeSource);
					break;
				case 'bookmark':
					// ..."bookmarked this"
					p.appendChild(document.createTextNode(' '));
					var bookmarkSource = document.createElement('a');
					bookmarkSource.appendChild(document.createTextNode('bookmarked this'));
					bookmarkSource.setAttribute('href', link.data.url);
					p.appendChild(bookmarkSource);
					break;
				default:
					// ..."sent a "...
					p.appendChild(document.createTextNode(' sent a '));
					// ..."Webmention notification"
					var mentionSource = document.createElement('a');
					mentionSource.appendChild(document.createTextNode('Webmention notification'));
					mentionSource.setAttribute('href', link.data.url);
					p.appendChild(mentionSource);
					break;
				}

				mentionsElement.appendChild(p);
			});
		})
		.catch(function(err) {
			console.log('Encountered Webmention error:', err);
			mentionsElement.appendChild(document.createTextNode('Couldn\'t fetch Webmentions. Try refreshing?'));
		});
	}

	document.addEventListener('DOMContentLoaded', init, false);
	var oldListener = document.onreadystatechange;
{'fraction_non_alphanumeric': 0.13284555712270804, 'fraction_numerical': 0.004407616361071932, 'mean_word_length': 3.6268352365415986, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, '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': '1033341', 'n_tokens_mistral': 3975, 'n_tokens_neox': 3205, 'n_words': 833}