86 lines
3.0 KiB
C#
86 lines
3.0 KiB
C#
using OnlyOneAccessTemplate.Services.OnlyOneAccessTemplate.Services;
|
|
using System.Text.RegularExpressions;
|
|
using System.Text;
|
|
|
|
namespace OnlyOneAccessTemplate.Services
|
|
{
|
|
public class UpperLowerConversorService : BaseConverterService
|
|
{
|
|
public override string ConverterType => "text-case-sentence";
|
|
|
|
public override string ConverterName => "Maiúsculas para minúsculas";
|
|
|
|
public UpperLowerConversorService(ILogger<UpperLowerConversorService> logger, IConfiguration configuration)
|
|
: base(logger, configuration) { }
|
|
|
|
public override async Task<ConversionResult> ConvertAsync(ConversionRequest request)
|
|
{
|
|
try
|
|
{
|
|
// Sua lógica de conversão aqui
|
|
var resultado = ConvertToSentenceCase(request.TextInput);
|
|
|
|
return new ConversionResult(true, OutputText: resultado);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Erro na conversão");
|
|
return new ConversionResult(false, ErrorMessage: "Erro ao processar");
|
|
}
|
|
}
|
|
|
|
private string ConvertToSentenceCase(string text)
|
|
{
|
|
if (string.IsNullOrEmpty(text))
|
|
return text;
|
|
|
|
// Converte todo o texto para minúsculas primeiro
|
|
string lowerText = text.ToLower();
|
|
|
|
// StringBuilder para construir o resultado
|
|
StringBuilder result = new StringBuilder(lowerText);
|
|
|
|
// Capitaliza a primeira letra do texto se for uma letra
|
|
if (result.Length > 0 && char.IsLetter(result[0]))
|
|
{
|
|
result[0] = char.ToUpper(result[0]);
|
|
}
|
|
|
|
// Regex para encontrar início de frases/parágrafos
|
|
// Procura por pontos, exclamações, interrogações ou quebras de linha
|
|
// seguidos por espaços e uma letra
|
|
string pattern = @"([.!?\n]\s*)([a-z])";
|
|
|
|
string resultText = result.ToString();
|
|
resultText = Regex.Replace(resultText, pattern, match =>
|
|
{
|
|
return match.Groups[1].Value + char.ToUpper(match.Groups[2].Value[0]);
|
|
});
|
|
|
|
return resultText;
|
|
}
|
|
|
|
public override ConverterConfiguration GetConfiguration(string language)
|
|
{
|
|
var texts = GetLocalizedTexts(language);
|
|
|
|
// Personalize os textos para seu conversor
|
|
texts["ConverterTitle"] = language switch
|
|
{
|
|
"en" => "Sentence Case Convert",
|
|
"es" => "TÍTULO DE TU CONVERTIDOR",
|
|
_ => "Converter para primeira maiúscula"
|
|
};
|
|
|
|
return new ConverterConfiguration
|
|
{
|
|
ConverterType = "text", // ou "file" ou "url"
|
|
OutputType = "text", // ou "download" ou "preview"
|
|
HasAdvancedOptions = false,
|
|
AllowShare = true,
|
|
LocalizedTexts = texts
|
|
};
|
|
}
|
|
}
|
|
}
|