-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
1,277 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "C#: Noted", | ||
"type": "dotnet", | ||
"request": "launch", | ||
"projectPath": "${workspaceFolder}/src/Noted/Noted.csproj", | ||
"launchConfigurationId": "TargetFramework=;Noted" | ||
}, | ||
{ | ||
"name": "C#: Launch (console)", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "dotnet: build", | ||
"program": "${workspaceFolder}/src/Noted/bin/Debug/net8.0/Noted.dll", | ||
"args": ["test/assets/koreader", "/tmp"] | ||
}, | ||
] | ||
} |
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,12 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "dotnet", | ||
"task": "build", | ||
"group": "build", | ||
"problemMatcher": [], | ||
"label": "dotnet: build" | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
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,83 @@ | ||
// Copyright (c) Arun Mahapatra. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Noted.Core.Models; | ||
|
||
using System; | ||
using System.Text.RegularExpressions; | ||
|
||
public struct EpubXPathLocation(string pos0, string pos1) : IComparable | ||
{ | ||
public EpubLocation Start { get; init; } = EpubLocation.FromString(pos0); | ||
|
||
public EpubLocation End { get; init; } = EpubLocation.FromString(pos1); | ||
|
||
public static EpubXPathLocation FromString(string location) | ||
{ | ||
var range = new Uri(location).PathAndQuery.Split('-'); | ||
return new EpubXPathLocation(range[0], range[1]); | ||
} | ||
|
||
public override string ToString() => $"epubxpath://{this.Start}-{this.End}"; | ||
|
||
public int CompareTo(object? obj) | ||
{ | ||
if (obj is not EpubXPathLocation other) | ||
{ | ||
throw new ArgumentException(null, nameof(obj)); | ||
} | ||
|
||
var startCompare = this.Start.CompareTo(other.Start); | ||
return startCompare == 0 ? this.End.CompareTo(other.End) : startCompare; | ||
} | ||
} | ||
|
||
public partial record EpubLocation( | ||
int DocumentFragmentId, | ||
string XPath, | ||
int CharacterLocation) : IComparable | ||
{ | ||
public int CompareTo(object? obj) | ||
{ | ||
if (obj is not EpubLocation other) | ||
{ | ||
throw new ArgumentException(null, nameof(obj)); | ||
} | ||
|
||
var docFragmentCompare = this.DocumentFragmentId.CompareTo(other.DocumentFragmentId); | ||
if (docFragmentCompare != 0) | ||
{ | ||
return docFragmentCompare; | ||
} | ||
|
||
// Comparing xpaths is impossible :( We'll take a chance to compare lexically, assuming the structure of book pages to be consistent. | ||
// TODO: It is better to probably keep the original order of elements. | ||
var xpathCompare = this.XPath.CompareTo(other.XPath); | ||
if (xpathCompare != 0) | ||
{ | ||
return xpathCompare; | ||
} | ||
|
||
return this.CharacterLocation.CompareTo(other.CharacterLocation); | ||
} | ||
|
||
public override string ToString() => $"/body/DocFragment[{this.DocumentFragmentId}]{this.XPath}.{this.CharacterLocation}"; | ||
|
||
public static EpubLocation FromString(string xpath) | ||
{ | ||
var match = EpubXPathRegex().Match(xpath); | ||
if (!match.Success || | ||
!int.TryParse(match.Groups["docFragmentId"].Value, out var docFragmentId) || | ||
string.IsNullOrEmpty(match.Groups["xpath"].Value) || | ||
!int.TryParse(match.Groups["charIndex"].Value, out var charIndex)) | ||
{ | ||
throw new ArgumentException( | ||
$"Invalid xpath: {xpath}", nameof(xpath)); | ||
} | ||
|
||
return new(docFragmentId, match.Groups["xpath"].Value, charIndex); | ||
} | ||
|
||
[GeneratedRegex(@"/body/DocFragment\[(?<docFragmentId>\d+)\](?<xpath>.*)\.(?<charIndex>.*)$", RegexOptions.Compiled)] | ||
private static partial Regex EpubXPathRegex(); | ||
} |
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
Oops, something went wrong.