<commit_before>﻿using System;
using System.Text;
using System.Xml;
using CommunityServer.Components;
using CommunityServer.Wikis.Components;

namespace Telligent.Evolution.TableOfContents
{
	public class TableOfContentsModule : ICSModule
	{
		private readonly ITableOfContentsService _tableOfContentsService;
		private readonly ITableOfContentsBuilder _tableOfContentsBuilder;
		public TableOfContentsModule()
		{
			_tableOfContentsService = Telligent.Common.Services.Get<ITableOfContentsService>();
			_tableOfContentsBuilder = Telligent.Common.Services.Get<ITableOfContentsBuilder>();
		}

		public TableOfContentsModule(ITableOfContentsService tableOfContentsService, ITableOfContentsBuilder tableOfContentsBuilder)
		{
			_tableOfContentsService = tableOfContentsService;
			_tableOfContentsBuilder = tableOfContentsBuilder;
		}

		public void Init(CSApplication csa, XmlNode node)
		{
			WikiEvents.BeforeAddPage += WikiEvents_BeforePageChange;
			WikiEvents.BeforeUpdatePage += WikiEvents_BeforePageChange;

			WikiEvents.RenderPage += WikiEvents_RenderViewableContent;
			WikiEvents.RenderPageRevision += WikiEvents_RenderViewableContent;
		}

		void WikiEvents_BeforePageChange(Page page, EventArgs e)
		{
			// Whenever saving articles to the DB, make sure all headings have anchors
			page.Body = _tableOfContentsService.EnsureHeadersHaveAnchors(page.Body);
		}

		void WikiEvents_RenderViewableContent(IViewableContent content, RenderEventArgs e)
		{
			e.RenderedContent = InsertTableOfContents(e.RenderedContent);
		}


		public string InsertTableOfContents(string html)
		{
			var position = GetTableOfContentsPosition(ref html);

			if (position == -1)
				return html;

			var hierarchy = _tableOfContentsService.GetHeadingHierarchy(html);

			var tableOfContents = _tableOfContentsBuilder.BuildTableOfContents(hierarchy);

			if (String.IsNullOrEmpty(tableOfContents))
				return html;<commit_msg>Updated table of contents module to support Forums, Blogs and Media Galleries.<commit_after>﻿using System;
using System.Text;
using System.Xml;
using CommunityServer.Components;
using CommunityServer.Wikis.Components;
using Services = Telligent.Common.Services;

namespace Telligent.Evolution.TableOfContents
{
	public class TableOfContentsModule : ICSModule
	{
		private readonly ITableOfContentsService _tableOfContentsService;
		private readonly ITableOfContentsBuilder _tableOfContentsBuilder;

		public TableOfContentsModule() : this(Services.Get<ITableOfContentsService>(), Services.Get<ITableOfContentsBuilder>())
		{
		}

		public TableOfContentsModule(ITableOfContentsService tableOfContentsService, ITableOfContentsBuilder tableOfContentsBuilder)
		{
			_tableOfContentsService = tableOfContentsService;
			_tableOfContentsBuilder = tableOfContentsBuilder;
		}

		public void Init(CSApplication csa, XmlNode node)
		{
			WikiEvents.BeforeAddPage += WikiEvents_BeforePageChange;
			WikiEvents.BeforeUpdatePage += WikiEvents_BeforePageChange;
			csa.PrePostUpdate += csa_PrePostUpdate;

			WikiEvents.RenderPage += WikiEvents_RenderViewableContent;
			WikiEvents.RenderPageRevision += WikiEvents_RenderViewableContent;
			csa.PreRenderPost += csa_PreRenderPost;
		}


		private void EnsureHeadersHaveAnchors(IContent content)
		{
			content.Body = _tableOfContentsService.EnsureHeadersHaveAnchors(content.Body);
			content.FormattedBody = _tableOfContentsService.EnsureHeadersHaveAnchors(content.FormattedBody);
		}

		private void csa_PrePostUpdate(IContent content, CSPostEventArgs e)
		{
			if (e.State != ObjectState.Create && e.State != ObjectState.Update)
				return;

			EnsureHeadersHaveAnchors(content);
		}

		private void WikiEvents_BeforePageChange(Page page, EventArgs e)
		{
			EnsureHeadersHaveAnchors(page);
		}


		void WikiEvents_RenderViewableContent(IViewableContent content, RenderEventArgs e)
		{
			e.RenderedContent = InsertTableOfContents(e.RenderedContent);
		}

		void csa_PreRenderPost(IContent content, CSPostEventArgs e)
		{
			content.FormattedBody = InsertTableOfContents(content.FormattedBody);
		}


		internal string InsertTableOfContents(string html)
		{
			var position = GetTableOfContentsPosition(ref html);

			if (position == -1)
				return html;

			var hierarchy = _tableOfContentsService.GetHeadingHierarchy(html);

			var tableOfContents = _tableOfContentsBuilder.BuildTableOfContents(hierarchy);

			if (String.IsNullOrEmpty(tableOfContents))
				return html;
{'fraction_non_alphanumeric': 0.06978319783197832, 'fraction_numerical': 0.00045167118337850043, 'mean_word_length': 6.075079872204473, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4319223', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1258, 'n_words': 235}