Skip to content

Commit

Permalink
refactor: render_with_features()
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Sep 15, 2024
1 parent 7fabaca commit 29582a7
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions md2zhihu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,28 +1202,26 @@ def rules_to_features(rules):


# features: {typ:action(), typ2:{subtyp:action()}}
def render_with_features(mdrender, rnode, features=None):
def render_with_features(mdrender, rnode: RenderNode, features=dict) -> Optional[List[str]]:
n = rnode.node

typ = n['type']

f = features
node_type = n['type']

if typ not in f:
if node_type not in features:
return None

f = f[typ]
if callable(f):
return f(mdrender, rnode)
type_handler = features[node_type]
if callable(type_handler):
return type_handler(mdrender, rnode)

# subtype is info
# subtype is info, the type after "```"
lang = n['info'] or ''

if lang in f:
return f[lang](mdrender, rnode)
if lang in type_handler:
return type_handler[lang](mdrender, rnode)

if '*' in f:
return f['*'](mdrender, rnode)
if '*' in type_handler:
return type_handler['*'](mdrender, rnode)

return None

Expand Down

0 comments on commit 29582a7

Please sign in to comment.