Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : URI detector/template loading from windows fs #110

Closed

Conversation

dgautier
Copy link

No description provided.

@dgautier dgautier marked this pull request as draft October 18, 2023 09:01
@dgautier dgautier changed the title fix : log error about source file when getting InvalidPathException fix : URI detector/template loading from windows fs Oct 18, 2023
@dgautier dgautier marked this pull request as ready for review October 18, 2023 12:19
@dgautier dgautier marked this pull request as draft October 18, 2023 12:40
@dgautier dgautier force-pushed the fix/logging-invalidpathexception branch from 1bcbbc3 to 2629949 Compare October 18, 2023 13:16
@dgautier dgautier marked this pull request as ready for review October 18, 2023 13:19
@@ -27,9 +28,10 @@ class AsciidocParser(
}

private val templatesLocation: Path by lazy {
val templateResources = AsciidocParser::class.java.getResource(TEMPLATES_LOCATION)!!.toURI()
val templateResources = AsciidocParser::class.java.classLoader.getResource(TEMPLATES_LOCATION)!!.toURI()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the reason for this change?

@@ -70,6 +71,7 @@ class PageContentTest {

@Test
internal fun `Invalid result for unbalanced xml`() {
Locale.setDefault(Locale.ENGLISH);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bad idea - especially taking into account that you do it without any try/finally/extension way.

It's better to use something like this https://wesome.org/junit-5-pioneer-defaultlocale or create adhoc extension in tests to do something similar

private val logger = KotlinLogging.logger {}
}
fun isValid(url: String): Boolean {
val pattern = Pattern.compile(URI_DETECTOR, Pattern.CASE_INSENSITIVE);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use regex as a field instead to avoid compiling regex string every time. Original code worked better, so please rollback to original approach.

}

private val normalizedDocs =
documents.map { (path, header) -> path.relativeTo(basePath).normalize() to header }.toMap()

override fun resolveReference(source: Path, refTo: String): Reference? {
if (URI_DETECTOR.matches(refTo)) return null

if (refTo.startsWith(MAILTO_DETECTOR)) return null
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't get what's the reason of doing multiple matches - how about simply slightly adjusting original regex to catch url scheme and checking that it's http, https, ftp or mailto?


if (refTo.startsWith(MAILTO_DETECTOR)) return null
if (refTo.startsWith(LOCALHOST_DETECTOR)) return null
if (isValid(refTo)) return null
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method name is confusing - what does "valid" mean here? better to rename with isExternalUrl or something like this

val document = normalizedDocs[targetPath]?.title ?: return null
return Xref(document, anchor)
} catch (ex: InvalidPathException) {
logger.error { "Failed to resolve : $refTo from $source" }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a bad pattern - log error and rethrow exception - because you'd get one more logging in another place. Either do not log or throw another exception with chained root cause.



@Test
internal fun `Http resolution`() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR title says that there is some fix for windows. What actually is failing here on windows?

@zeldigas
Copy link
Owner

zeldigas commented Nov 4, 2023

Fixed in #116. Differences:

  1. more accurate resolution of templates
  2. fixed regex instead of maintaining multiple ones for external links detection

@zeldigas zeldigas closed this Nov 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants