-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conversion to YAML as well as Markdown for JSON
This makes it easier to index and query the data depending on the target functionality.
- Loading branch information
Showing
19 changed files
with
11,870 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Clarius.OpenLaw; | ||
|
||
public class AssistantOptions | ||
{ | ||
/// <summary> | ||
/// The assistant id. | ||
/// </summary> | ||
public required string Id { get; set; } | ||
/// <summary> | ||
/// The assistant access key/token. | ||
/// </summary> | ||
public required string Key { get; set; } | ||
/// <summary> | ||
/// The assistant endpoint uri. | ||
/// </summary> | ||
public required string Uri { get; set; } | ||
/// <summary> | ||
/// The assistant vector store identifier. | ||
/// </summary> | ||
public required string Store { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.ComponentModel; | ||
using System.Text.Json; | ||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace Clarius.OpenLaw; | ||
|
||
[Description("Convierte archivos JSON a YAML y Markdown.")] | ||
public class ConvertCommand(IAnsiConsole console) : Command<ConvertCommand.ConvertSettings> | ||
{ | ||
public override int Execute(CommandContext context, ConvertSettings settings) | ||
{ | ||
if (settings.File is not null) | ||
{ | ||
ConvertFile(settings.File, true); | ||
return 0; | ||
} | ||
|
||
if (Directory.Exists(settings.Directory)) | ||
{ | ||
console.Progress() | ||
.Columns( | ||
[ | ||
new TaskDescriptionColumn(), | ||
new ProgressBarColumn(), | ||
]) | ||
.Start(ctx => | ||
{ | ||
Parallel.ForEach(Directory.EnumerateFiles(settings.Directory, "*.json", SearchOption.AllDirectories), file => | ||
{ | ||
var task = ctx.AddTask($"Convirtiendo {file}"); | ||
task.IsIndeterminate = true; | ||
ConvertFile(file, true); | ||
task.Value(100); | ||
}); | ||
}); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static void ConvertFile(string file, bool overwrite) => DictionaryConverter.ConvertFile(file, overwrite); | ||
|
||
public class ConvertSettings : CommandSettings | ||
{ | ||
public override ValidationResult Validate() | ||
{ | ||
if (!string.IsNullOrWhiteSpace(File) && !System.IO.File.Exists(File)) | ||
return ValidationResult.Error("El archivo especificado '{File}' no existe."); | ||
|
||
return base.Validate(); | ||
} | ||
|
||
[Description("Archivo a convertir. Opcional.")] | ||
[CommandArgument(0, "[file]")] | ||
public string? File { get; set; } | ||
|
||
[Description("Ubicación de archivos a convertir. Por defecto '%AppData%\\clarius\\openlaw'")] | ||
[CommandOption("--dir")] | ||
public string Directory { get; set; } = Environment.ExpandEnvironmentVariables("%AppData%\\clarius\\openlaw"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace Clarius.OpenLaw; | ||
|
||
public class DictionaryConverter | ||
{ | ||
static readonly JsonSerializerOptions options = new() | ||
{ | ||
Converters = { new JsonDictionaryConverter() }, | ||
}; | ||
|
||
public static void ConvertFile(string jsonFile, bool overwrite) | ||
{ | ||
var yamlDir = Path.Combine(Path.GetDirectoryName(jsonFile) ?? "", "yaml"); | ||
var yamlFile = Path.Combine(yamlDir, Path.ChangeExtension(Path.GetFileName(jsonFile), ".yaml")); | ||
Directory.CreateDirectory(yamlDir); | ||
|
||
var mdDir = Path.Combine(Path.GetDirectoryName(jsonFile) ?? "", "md"); | ||
var mdFile = Path.Combine(mdDir, Path.ChangeExtension(Path.GetFileName(jsonFile), ".md")); | ||
Directory.CreateDirectory(mdDir); | ||
|
||
Dictionary<string, object?>? dictionary = null; | ||
|
||
if (overwrite || !File.Exists(yamlFile)) | ||
{ | ||
dictionary = Parse(File.ReadAllText(jsonFile)); | ||
if (dictionary is null) | ||
return; | ||
|
||
File.WriteAllText(yamlFile, ToYaml(dictionary), Encoding.UTF8); | ||
} | ||
|
||
if (overwrite || !File.Exists(mdFile)) | ||
{ | ||
if (dictionary is null) | ||
dictionary = Parse(File.ReadAllText(jsonFile)); | ||
if (dictionary is null) | ||
return; | ||
|
||
File.WriteAllText(mdFile, ToMarkdown(dictionary), Encoding.UTF8); | ||
} | ||
} | ||
|
||
public static Dictionary<string, object?>? Parse(string json) | ||
=> JsonSerializer.Deserialize<Dictionary<string, object?>>(json, options); | ||
|
||
public static string ToYaml(Dictionary<string, object?> dictionary) | ||
{ | ||
var serializer = new SerializerBuilder() | ||
.WithTypeConverter(new YamlDictionaryConverter()) | ||
.WithTypeConverter(new YamlListConverter()) | ||
.Build(); | ||
|
||
return serializer.Serialize(dictionary); | ||
} | ||
|
||
public static string ToMarkdown(Dictionary<string, object?> dictionary) | ||
{ | ||
var output = new StringBuilder(); | ||
ProcessDictionary(0, dictionary!, output); | ||
return output.ToString(); | ||
} | ||
|
||
static void ProcessObject(int depth, object? obj, StringBuilder output) | ||
{ | ||
if (obj is Dictionary<string, object?> dictionary) | ||
{ | ||
ProcessDictionary(depth, dictionary, output); | ||
} | ||
else if (obj is List<object?> list) | ||
{ | ||
foreach (var item in list) | ||
{ | ||
ProcessObject(depth, item, output); | ||
} | ||
} | ||
} | ||
|
||
static void ProcessDictionary(int depth, Dictionary<string, object?> dictionary, StringBuilder output) | ||
{ | ||
var title = dictionary | ||
.Where(x => x.Key.StartsWith("titulo-", StringComparison.OrdinalIgnoreCase)) | ||
.FirstOrDefault().Value; | ||
|
||
if (title is not null) | ||
{ | ||
depth++; | ||
output.AppendLine().AppendLine($"{new string('#', depth)} {title}"); | ||
} | ||
|
||
foreach (var kvp in dictionary) | ||
{ | ||
var key = kvp.Key; | ||
var value = kvp.Value; | ||
if (value is null) | ||
continue; | ||
|
||
if (key == "texto" && | ||
// We may have section title with text without an article # | ||
(dictionary.ContainsKey("numero-articulo") || title is not null)) | ||
{ | ||
output.AppendLine().AppendLine(value.ToString()); | ||
} | ||
else | ||
{ | ||
ProcessObject(depth, value, output); | ||
} | ||
} | ||
|
||
if (title is not null) | ||
{ | ||
depth--; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.ComponentModel; | ||
using System.Text.Json; | ||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
|
||
namespace Clarius.OpenLaw; | ||
|
||
[Description("Normaliza el formato de archivos JSON.")] | ||
public class FormatCommand(IAnsiConsole console) : Command<FormatCommand.FormatSettings> | ||
{ | ||
static readonly JsonSerializerOptions readOptions = new() | ||
{ | ||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, | ||
Converters = { new JsonDictionaryConverter() }, | ||
}; | ||
|
||
static readonly JsonSerializerOptions writeOptions = new() | ||
{ | ||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, | ||
WriteIndented = true, | ||
}; | ||
|
||
public class FormatSettings : CommandSettings | ||
{ | ||
[Description("Ubicación opcional para descarga de archivos. Por defecto '%AppData%\\clarius\\openlaw'")] | ||
[CommandOption("--dir")] | ||
public string Directory { get; set; } = Environment.ExpandEnvironmentVariables("%AppData%\\clarius\\openlaw"); | ||
} | ||
|
||
public override int Execute(CommandContext context, FormatSettings settings) | ||
{ | ||
if (Directory.Exists(settings.Directory)) | ||
{ | ||
console.Progress() | ||
.Columns( | ||
[ | ||
new TaskDescriptionColumn(), | ||
new ProgressBarColumn(), | ||
]) | ||
.Start(ctx => | ||
{ | ||
Parallel.ForEach(Directory.EnumerateFiles(settings.Directory, "*.json", SearchOption.AllDirectories), file => | ||
{ | ||
var task = ctx.AddTask($"Formateando {file}"); | ||
task.IsIndeterminate = true; | ||
FormatFile(file); | ||
task.Value(100); | ||
}); | ||
}); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void FormatFile(string file) | ||
{ | ||
var json = File.ReadAllText(file); | ||
var dictionary = JsonSerializer.Deserialize<Dictionary<string, object?>>(json, readOptions); | ||
File.WriteAllText(file, JsonSerializer.Serialize(dictionary, writeOptions)); | ||
} | ||
} |
Oops, something went wrong.