commonmark / commonmark/commonmark-java

Implement methods to go backwards and look backwards inside Scanner

未关闭
#395 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

enhancement
主要语言
Java
星标
2.7k
派生
336
PR 合并指标
30 天内没有已合并 PR

描述

Is your feature request related to a problem? Please describe.
I'm trying to implement an InlineContentParser for images from a custom markdown spec from a certain website. Given I already parsed the opener, the spec requires me to look for the first whitespace character or end of text, then going backwards from there, look for the first ). Everything between the opener and ) is considered the image link.

This is a bit annoying to implement with the current limited set of methods Scanner has, and I also imagine the performance would be worse when instead of going backwards from the closer, you go forward from the opener via scanner.find(')') and perform an additional find to see if you just found the last ).

Describe the solution you'd like
I would like for there to be similar methods to next and find that would instead look back from the current Scanner position, i.e prev, and findPrev. Then I could just do the following (sorry that the code is in Kotlin as I don't regularly write Java, I hope the intent is clear):

val start = scanner.position()  // position after the opener has been parsed
scanner.find { char -> char.isWhitespace() } // scanner gets moved to first whitespace or very end if there is none
scanner.findPrev(')') // scanner gets moved backwards until the position is at ')'. Ideally there would also be an overload that takes a position until which it should move, so i could pass the `start` to it and it would early exit if it doesn't find one by then
val end = scanner.position() 
val imageLink = scanner.getSource(start, end).getContent()

Describe alternatives you've considered
As stated above I have considered iterating forward from the current position to find the next ) and check if there's another one to see if I'd found the last one.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Scanner 入口点开始,比较 issue 中描述的现有 next 和 find 行为。定义按要求更新 scanner 位置的向后等价功能,包括有界搜索行为,并验证 image-link 解析用例能够获取预期范围。

由索引模型根据 Issue 内容生成。

评估

技术栈
java
领域
compilers
Issue 类型
功能
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。