<commit_before>// Copyright 2013 <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package mapper

import (
	"fmt"
	"github.com/andreaskoch/allmark/converter"
	"github.com/andreaskoch/allmark/parser"
	"github.com/andreaskoch/allmark/path"
	"github.com/andreaskoch/allmark/repository"
	"github.com/andreaskoch/allmark/view"
	"regexp"
	"time"
)

// Pattern which matches all HTML/XML tags
var HtmlTagPattern = regexp.MustCompile(`\<[^\>]*\>`)

func createMessageMapperFunc(pathProvider *path.Provider, targetFormat string) Mapper {
	return func(item *repository.Item) view.Model {

		parsed, err := converter.Convert(item, targetFormat)
		if err != nil {
			return view.Error(fmt.Sprintf("%s", err))
		}

		return view.Model{
			Path:        pathProvider.GetWebRoute(item),
			Title:       getTitle(parsed),
			Description: getDescription(parsed),
			Content:     parsed.ConvertedContent,
			LanguageTag: getTwoLetterLanguageCode(parsed.MetaData.Language),
		}
	}
}

func getDescription(parsedResult *parser.Result) string {
	return parsedResult.MetaData.Date.Format(time.RFC850)
}

func getTitle(parsedResult *parser.Result) string {
	text := HtmlTagPattern.ReplaceAllString(parsedResult.ConvertedContent, "")
	excerpt := getTextExcerpt(text, 30)
	time := parsedResult.MetaData.Date.Format(time.RFC850)

	return fmt.Sprintf("%s ◦ %s", excerpt, time)
}

func getTextExcerpt(text string, length int) string {

	if len(text) <= length {
		return text<commit_msg>Changed the title format for messages<commit_after>// Copyright 2013 <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package mapper

import (
	"fmt"
	"github.com/andreaskoch/allmark/converter"
	"github.com/andreaskoch/allmark/parser"
	"github.com/andreaskoch/allmark/path"
	"github.com/andreaskoch/allmark/repository"
	"github.com/andreaskoch/allmark/view"
	"regexp"
	"time"
)

// Pattern which matches all HTML/XML tags
var HtmlTagPattern = regexp.MustCompile(`\<[^\>]*\>`)

func createMessageMapperFunc(pathProvider *path.Provider, targetFormat string) Mapper {
	return func(item *repository.Item) view.Model {

		parsed, err := converter.Convert(item, targetFormat)
		if err != nil {
			return view.Error(fmt.Sprintf("%s", err))
		}

		return view.Model{
			Path:        pathProvider.GetWebRoute(item),
			Title:       getTitle(parsed),
			Description: getDescription(parsed),
			Content:     parsed.ConvertedContent,
			LanguageTag: getTwoLetterLanguageCode(parsed.MetaData.Language),
		}
	}
}

func getDescription(parsedResult *parser.Result) string {
	return parsedResult.MetaData.Date.Format(time.RFC850)
}

func getTitle(parsedResult *parser.Result) string {
	text := HtmlTagPattern.ReplaceAllString(parsedResult.ConvertedContent, "")
	excerpt := getTextExcerpt(text, 30)
	time := parsedResult.MetaData.Date.Format(time.RFC850)

	return fmt.Sprintf("%s: %s", time, excerpt)
}

func getTextExcerpt(text string, length int) string {

	if len(text) <= length {
		return text
{'fraction_non_alphanumeric': 0.12294818152558738, 'fraction_numerical': 0.007724493080141615, 'mean_word_length': 4.66120218579235, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1601514', 'n_tokens_mistral': 1077, 'n_tokens_neox': 1015, 'n_words': 227}