<commit_before>package org.wallride.core.support;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
import org.thymeleaf.context.IProcessingContext;
import org.wallride.core.domain.*;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//@Component
public class PostUtils {

//	@Inject
//	private SettingService settingService;
//
//	@Inject
//	private DefaultModelAttributeService defaultModelAttributeService;
//
//	@Inject
//	private Environment environment;

	private IProcessingContext processingContext;

	private Settings settings;

//	private Environment environment;

	public PostUtils(IProcessingContext processingContext, Settings settings) {
		this.processingContext = processingContext;
		this.settings = settings;
//		this.environment = environment;
	}

	public String link(Article article) {
		UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
		return path(builder, article);
	}

	public String link(Page page, PageTree pageTree) {
		UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
		return path(builder, page, pageTree);
	}

	public String path(Article article) {
		UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
		return path(builder, article);
	}

	public String path(Page page, PageTree pageTree) {
		UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
		return path(builder, page, pageTree);
	}

	private String path(UriComponentsBuilder builder, Article article) {
		Map<String, Object> params = new HashMap<>();
		String[] languages = (String[]) processingContext.getContext().getVariables().get("LANGUAGES");
		if (languages != null && languages.length > 1) {
			builder.path("/{language}");
			params.put("language", processingContext.getContext().getLocale().getLanguage());
		}
		builder.path("/{year}/{month}/{day}/{code}");
		params.put("year", String.format("%04d", article.getDate().getYear()));
		params.put("month", String.format("%02d", article.getDate().getMonthOfYear()));
		params.put("day", String.format("%02d", article.getDate().getDayOfMonth()));
		params.put("code", article.getCode());
		return builder.buildAndExpand(params).toUriString();
	}

	private String path(UriComponentsBuilder builder, Page page, PageTree pageTree) {
		Map<String, Object> params = new HashMap<>();
		String[] languages = (String[]) processingContext.getContext().getVariables().get("LANGUAGES");
		if (languages != null && languages.length > 1) {
			builder.path("/{language}");
			params.put("language", processingContext.getContext().getLocale().getLanguage());
		}

		pageTree = (PageTree) processingContext.getContext().getVariables().get("PAGE_TREE_ALL");
//		PageTree pageTree = defaultModelAttributeService.readPageTree(LocaleContextHolder.getLocale().getLanguage());
		List<String> codes = new LinkedList<>();
		Page parent = page.getParent();
		while (parent != null) {
			codes.add(parent.getCode());
			parent = (parent.getParent() != null) ? pageTree.getPageByCode(parent.getParent().getCode()) : null;
		}

		Collections.reverse(codes);
		codes.add(page.getCode());

		for (int i = 0; i < codes.size(); i++) {
			String key = "code" + i;
			builder.path("/{" + key + "}");
			params.put(key, codes.get(i));
		}
		return builder.buildAndExpand(params).toUriString();
	}

	public String metaKeywords(Post post) {
		return null; //TODO
	}

	public String metaAuthor(Post post) {
		return null; //TODO
	}

	public String metaDescription(Post post) {
		return null; //TODO
	}

	public String ogSiteName(Post post) {
		return settings.readSettingAsString(Setting.Key.WEBSITE_TITLE, processingContext.getContext().getLocale().getLanguage());
	}

	public String ogTitle(Post post) {
		return post.getTitle();<commit_msg>Bugfix: posts url encode<commit_after>package org.wallride.core.support;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
import org.thymeleaf.context.IProcessingContext;
import org.wallride.core.domain.*;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//@Component
public class PostUtils {

//	@Inject
//	private SettingService settingService;
//
//	@Inject
//	private DefaultModelAttributeService defaultModelAttributeService;
//
//	@Inject
//	private Environment environment;

	private IProcessingContext processingContext;

	private Settings settings;

//	private Environment environment;

	public PostUtils(IProcessingContext processingContext, Settings settings) {
		this.processingContext = processingContext;
		this.settings = settings;
//		this.environment = environment;
	}

	public String link(Article article) {
		UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
		return path(builder, article);
	}

	public String link(Page page, PageTree pageTree) {
		UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
		return path(builder, page, pageTree);
	}

	public String path(Article article) {
		UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
		return path(builder, article);
	}

	public String path(Page page, PageTree pageTree) {
		UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
		return path(builder, page, pageTree);
	}

	private String path(UriComponentsBuilder builder, Article article) {
		Map<String, Object> params = new HashMap<>();
		String[] languages = (String[]) processingContext.getContext().getVariables().get("LANGUAGES");
		if (languages != null && languages.length > 1) {
			builder.path("/{language}");
			params.put("language", processingContext.getContext().getLocale().getLanguage());
		}
		builder.path("/{year}/{month}/{day}/{code}");
		params.put("year", String.format("%04d", article.getDate().getYear()));
		params.put("month", String.format("%02d", article.getDate().getMonthOfYear()));
		params.put("day", String.format("%02d", article.getDate().getDayOfMonth()));
		params.put("code", article.getCode());
		return builder.buildAndExpand(params).encode().toUriString();
	}

	private String path(UriComponentsBuilder builder, Page page, PageTree pageTree) {
		Map<String, Object> params = new HashMap<>();
		String[] languages = (String[]) processingContext.getContext().getVariables().get("LANGUAGES");
		if (languages != null && languages.length > 1) {
			builder.path("/{language}");
			params.put("language", processingContext.getContext().getLocale().getLanguage());
		}

		pageTree = (PageTree) processingContext.getContext().getVariables().get("PAGE_TREE_ALL");
//		PageTree pageTree = defaultModelAttributeService.readPageTree(LocaleContextHolder.getLocale().getLanguage());
		List<String> codes = new LinkedList<>();
		Page parent = page.getParent();
		while (parent != null) {
			codes.add(parent.getCode());
			parent = (parent.getParent() != null) ? pageTree.getPageByCode(parent.getParent().getCode()) : null;
		}

		Collections.reverse(codes);
		codes.add(page.getCode());

		for (int i = 0; i < codes.size(); i++) {
			String key = "code" + i;
			builder.path("/{" + key + "}");
			params.put(key, codes.get(i));
		}
		return builder.buildAndExpand(params).encode().toUriString();
	}

	public String metaKeywords(Post post) {
		return null; //TODO
	}

	public String metaAuthor(Post post) {
		return null; //TODO
	}

	public String metaDescription(Post post) {
		return null; //TODO
	}

	public String ogSiteName(Post post) {
		return settings.readSettingAsString(Setting.Key.WEBSITE_TITLE, processingContext.getContext().getLocale().getLanguage());
	}

	public String ogTitle(Post post) {
		return post.getTitle();
{'fraction_non_alphanumeric': 0.1432449105490438, 'fraction_numerical': 0.002220851326341764, 'mean_word_length': 5.47444089456869, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '2009903', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2315, 'n_words': 492}