-
Notifications
You must be signed in to change notification settings - Fork 1
Parser #7
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
Parser #7
Changes from 3 commits
168c250
b79097d
68562dd
38127c9
fc8a6a3
aab0412
4bd8ec9
bbeb42d
5aab682
5dab5d2
98f148d
3548b03
5183dda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
from .elements import * | ||
from .html import CustomHTML | ||
from .markdown import CustomMarkdown | ||
from .rst import CustomRst | ||
from .parse import Collection, HTMLParser, MDParser, RSTParser | ||
|
||
def from_html(html:'str'): | ||
return Symbol.from_html(html) | ||
|
||
def from_md(md:'str'): | ||
return Symbol.from_md(md) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
from .symbol import Symbol | ||
|
||
from .a import A | ||
from .blockquote import Blockquote | ||
from .body import Body | ||
from .br import Br | ||
from .code import Code | ||
from .div import Div | ||
from .h import H1,H2,H3,H4,H5,H6 | ||
from .head import Head | ||
from .hr import Hr | ||
from .html import HTML | ||
from .img import Img | ||
from .input import Input | ||
from .li import OL, UL, LI | ||
from .text import Text | ||
from .div import Div | ||
from .p import P | ||
from .span import Span | ||
from .img import Img | ||
from .text_formatting import Strong, Em, Code | ||
from .br import Br | ||
from .blockquote import Blockquote | ||
from .hr import Hr | ||
from .table import Table, Tr, Td, Th | ||
from .input import Input | ||
from .code import Code | ||
from .table import Table, Tr, Td, Th, THead, TBody | ||
from .text_formatting import Strong, Em, Strong as B, Em as I | ||
from .text import Text | ||
from .title import Title |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,23 +1,52 @@ | ||||||||||||||||
from BetterMD.rst.custom_rst import CustomRst | ||||||||||||||||
from .symbol import Symbol | ||||||||||||||||
from ..rst import CustomRst | ||||||||||||||||
from ..markdown import CustomMarkdown | ||||||||||||||||
from ..html import CustomHTML | ||||||||||||||||
import re | ||||||||||||||||
import typing as t | ||||||||||||||||
|
||||||||||||||||
class MD(CustomMarkdown['A']): | ||||||||||||||||
if t.TYPE_CHECKING: | ||||||||||||||||
from ..parse import Collection | ||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
class MD(CustomMarkdown): | ||||||||||||||||
def to_md(self, inner, symbol, parent): | ||||||||||||||||
return f"[{" ".join([e.to_md() for e in inner])}]({symbol.get_prop("href")})" | ||||||||||||||||
|
||||||||||||||||
def verify(self, text:'str'): | ||||||||||||||||
if re.findall("\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)", text): | ||||||||||||||||
# Case 1: Inline link | ||||||||||||||||
return True | ||||||||||||||||
|
||||||||||||||||
elif re.findall("<(https?:\/\/[^\s>]+)>", text): | ||||||||||||||||
# Case 2: Automatic Links | ||||||||||||||||
return True | ||||||||||||||||
|
||||||||||||||||
elif re.findall("\[([^\]]+)\]\[([^\]]+)\]\s*\n?\[([^\]]+)\]:\s*(https?:\/\/[^\s]+)", text): | ||||||||||||||||
# Case 3: Reference Links | ||||||||||||||||
return True | ||||||||||||||||
|
||||||||||||||||
return False | ||||||||||||||||
|
||||||||||||||||
class HTML(CustomHTML['A']): | ||||||||||||||||
def to_html(self, inner, symbol, parent): | ||||||||||||||||
return f"<a href={symbol.get_prop('href')}>{" ".join([e.to_html() for e in inner])}</a>" | ||||||||||||||||
|
||||||||||||||||
class RST(CustomRst['A']): | ||||||||||||||||
def to_rst(self, inner, symbol, parent): | ||||||||||||||||
return f"`{' '.join([e.to_rst() for e in inner])} <{symbol.get_prop('href')}>`_" | ||||||||||||||||
|
||||||||||||||||
class A(Symbol): | ||||||||||||||||
prop_list = ["href"] | ||||||||||||||||
|
||||||||||||||||
refs = {} | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use a class variable for shared references with caution. The -class A(Symbol):
- ...
- refs = {}
+class A(Symbol):
+ ...
+ # If you need a unique dictionary on each instance, move refs to __init__
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.refs = {} 📝 Committable suggestion
Suggested change
|
||||||||||||||||
md = MD() | ||||||||||||||||
html = HTML() | ||||||||||||||||
rst = RST() | ||||||||||||||||
html = "a" | ||||||||||||||||
rst = RST() | ||||||||||||||||
|
||||||||||||||||
@classmethod | ||||||||||||||||
def md_refs(cls, references: 'list[str]' = None): | ||||||||||||||||
pass | ||||||||||||||||
|
||||||||||||||||
@classmethod | ||||||||||||||||
def rst_refs(cls, references: 'list[str]' = None): | ||||||||||||||||
pass | ||||||||||||||||
|
||||||||||||||||
@classmethod | ||||||||||||||||
def html_refs(cls, references: 'list[str]' = None): | ||||||||||||||||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .symbol import Symbol | ||
|
||
|
||
class Body(Symbol): | ||
html = "body" | ||
md = "" | ||
rst = "" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .symbol import Symbol | ||
|
||
class HTML(Symbol): | ||
html = "html" | ||
md = "" | ||
rst = "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainConsider implementing conversion methods for consistency The class doesn't appear to have any methods for handling the conversion between formats. Based on the PR context, most Symbol subclasses likely implement methods like 🌐 Web query:
💡 Result: Symbol subclasses typically implement specialized methods for format conversion, leveraging programming language-specific mechanisms to control type coercion and string representation. These methods vary across ecosystems but share common patterns: 1. String Representation Overrides
Symbol('foo').toString(); // "Symbol(foo)"[10]
class CustomSymbol {
get [Symbol.toStringTag]() { return 'MySymbol'; }
}
Object.prototype.toString.call(new CustomSymbol()); // "[object MySymbol]"[12] 2. Format Specification Handlers
class MySymbol:
def __format__(self, format_spec):
return f"Formatted: {format_spec}" 3. Type Conversion Protocols
const sym = Symbol();
Number(sym); // TypeError: Cannot convert Symbol to number[10] 4. Custom Formatter Integration
f"{symbol!r}" # Calls repr() instead of str()[4] 5. Parser Interaction Methods
DecimalFormat df = new DecimalFormat("#,##0.00");
Number num = df.parse("1,234.56");[9] 6. Localization Support
public string ToString(string format, IFormatProvider provider) {
// Culture-specific formatting[13]
} These methods enable precise control over symbol representation while maintaining type safety. JavaScript Symbols particularly restrict implicit conversions, requiring explicit toString() calls[10][12], whereas Python and .NET provide more flexible formatting hooks through standardized interfaces[3][4][13]. The implementation details often involve overriding base class methods or implementing formatting interfaces like IFormattable[13]. Citations:
Conversion Methods Implementation Required The Suggested changes: class HTML(Symbol):
html = "html"
md = ""
rst = ""
def to_html(self):
return self.html
def to_md(self):
return self.md
def to_rst(self):
return self.rst |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,4 +4,9 @@ class P(Symbol): | |||||||||||||||
html = "p" | ||||||||||||||||
md = "" | ||||||||||||||||
rst = "\n\n" | ||||||||||||||||
nl = True | ||||||||||||||||
nl = True | ||||||||||||||||
|
||||||||||||||||
class Pre(Symbol): | ||||||||||||||||
html = "pre" | ||||||||||||||||
md = "" | ||||||||||||||||
rst = "" | ||||||||||||||||
Comment on lines
+9
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion New Pre class implementation appears incomplete The new Consider implementing proper Markdown and RST representations: class Pre(Symbol):
html = "pre"
- md = ""
- rst = ""
+ md = "```\n" # Triple backticks for code blocks in Markdown
+ rst = "::\n\n" # Double colon with double newline for preformatted blocks in RST
+ nl = True # Ensure a newline after the element like the P class 📝 Committable suggestion
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Symbol undefined in parsing functions
Both
from_html
andfrom_md
functions referenceSymbol
, but it's not explicitly imported. While it might be included via the star import from.elements
, this could lead to confusion and potential issues if the import structure changes.Make the dependency on
Symbol
explicit:from .elements import * +from .elements.symbol import Symbol # Explicitly import Symbol from .parse import Collection, HTMLParser, MDParser, RSTParser def from_html(html:'str'): return Symbol.from_html(html) def from_md(md:'str'): return Symbol.from_md(md)
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.8.2)
5-5:
Symbol
may be undefined, or defined from star imports(F405)
8-8:
Symbol
may be undefined, or defined from star imports(F405)