Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
マークダウンパーサーの自作
Search
suzakutakumi
December 11, 2021
Technology
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
マークダウンパーサーの自作
suzakutakumi
December 11, 2021
Transcript
22 slides
マークダウンパーサー の自作 2021/12/11 Zli 大LT
自己紹介 HN:朱雀 匠(本名:鈴木 拓眞) Twitter: @suzakutakumi3 Portfolio: http://suzakutakumi.mydns.jp/
None
None
マークダウンパーサーとは マークダウンをHTML等に変換してくれる奴です。
マークダウンを使うところ - Notion - Qiita - GitHub - esa.io -
VSCode
使ってて思ったこと - 色変えたい - 改行がスペース2個書かないとされないの意味わからん - 文字サイズも変更したい どうしたら良いのか?
自分で作る!
注意点! 構文解析とか文法解析とかは知らない 行きあたりばったりの何となくで作成しています
見出しの実装 if l>0 and row[0]=='#': cnt=1 while row[cnt]=='#': cnt+=1 if
row[cnt]==" ": row="<h%d>"%cnt+row[cnt+1:-4]+"</h%d>"%cnt これは1行:1行の対応なので、簡単に実装できました。
リンクの実装 ans=re.search(r"\[.+\]\(.+\)",row) if ans!=None: sl=ans.span() first=row[:sl[0]] second=row[sl[0]:sl[1]] last=row[sl[1]:] content=re.search(r"\[.+\]",second).span() link=re.search(r"\(.+\)",second).span()
row=first+'<a href="%s">%s</a>'%(second[link[0]+1:link[1]-1],second[content[0]+1:content[1]-1])+last こちらも1行:1行の対応なので、簡単に実装できました。 正規表現で、その行にリンクがあるか?リンクの位置などを抽出しています。
画像の実装 ans=re.search(r"!\[.*\]\(.+\)",row) if ans!=None: sl=ans.span() first=row[:sl[0]] second=row[sl[0]:sl[1]] last=row[sl[1]:] content=re.search(r"\[.+\]",second).span() link=re.search(r"\(.+\)",second).span()
row=first+'<img src="%s" alt="%s"/>'%(second[link[0]+1:link[1]-1],second[content[0]+1:content[1]-1])+last ほぼ、リンクと変わらないですね
リストの実装1 リストとは
リストの実装2 コードは省略 こちらは一番時間がかかっています。 リストのネストに対応しています。
サイズ変更の実装 ans=re.search(r"<[0-9]+>",row) if ans!=None: sl=ans.span() first=row[:sl[0]] second='<font size="%s">'%row[sl[0]+1:sl[1]-1] last=row[sl[1]:] row=first+second+last
fin_list.append("</font>") こちらも、正規表現を用いて、サイズの実装をしました。
色の実装 ans=re.search(r"<#[0-9a-fA-F]+>",row) if ans!=None: sl=ans.span() first=row[:sl[0]] second='<font color="%s">'%row[sl[0]+1:sl[1]-1] last=row[sl[1]:] row=first+second+last
fin_list.append("</font>") colorTexts=["black","gray","silver","white","blue","navy","teal","green","lime","aqua","yellow","red","fuchsia","olive"," purple","maroon"] for texts in colorTexts: ans=re.search("<%s>"%texts,row) if ans!=None: sl=ans.span() first=row[:sl[0]] second='<font color="%s">'%row[sl[0]+1:sl[1]-1] last=row[sl[1]:] row=first+second+last fin_list.append("</font>") こちらも正規表現を利用して、実装しています。
デモ
Webサーバでの実装 将来的には、自分のポートフォリオサイトで使いたいので、Webで編 集できるようにしたい!
Flaskで実装 from flask import Flask,render_template,request import sys sys.path.append( '../') from
command import MarkdownParser as MP app = Flask(__name__) @app.route('/') def Index(): return render_template( "index.html") @app.route('/test',methods=['GET','POST']) def Test(): data=request.get_data().decode() ans=MP.main(data.split( '\n')) return '\n'.join(ans) if __name__ == "__main__": app.run(debug=True) /testにアクセスすると、bodyの内容を 先ほど作ったマークダウンパーサーを呼び出し、変換しています。
デモ
今後について • シンタックスハイライトの対応やその他の便利機能を追加する • 構文解析などを習ったので、もう少ししっかりした設計にしたい • CSSをしっかりさせる • 自分のポートフォリオ用に適応させる
最後に 下のリンクからプログラムがおいてあるリポジトリを見ることができます。 https://github.com/suzakutakumi/MarkdownParser
More Decks by suzakutakumi
See All by suzakutakumi
ピクロス作成の中間発表
suzakutakumi
0
120
しゅみろん
suzakutakumi
0
200
trap-search
suzakutakumi
1
75
Pyramid Makerの作成
suzakutakumi
0
74
絵文字ジェネレータボットの作成
suzakutakumi
0
180
send_discord
suzakutakumi
0
76
独自ドメインについて
suzakutakumi
0
72
ESP32とAlexaを用いたエアコン制御
suzakutakumi
0
1.4k
スターリンマージソート
suzakutakumi
3
660
Other Decks in Technology
See All in Technology
AIエージェントのアーキテクチャ設計
nsatoh
0
370
セキュリティチームは どのコンテナイメージの脆弱性対応を優先すべきか 〜CISA新指針とSCCで作る脆弱性トリアージ〜
swdtkuy
0
210
異夢同船〜異なる夢でも同じ船でなんとかする〜
fortegp05
1
570
RAGの改善を速く回すための評価基盤
yosukeyoshida
6
3.4k
Well, that’s weird…
csswizardry
1
150
20261113_JAWS_Osaka
takuyay0ne
1
600
Test Impact Analysis を武器に立ち向かう、AI 時代のテスト増加に対するヌーラボのアプローチ / getting-started-datadog-test-impact-analysis
k6s4i53rx
0
270
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
17
120k
市販IoT機器や自作IoT機器とHomeAssistantを駆使して家の電気代をできる限り見える化する方法の考案
harutiro
0
400
OpenTelemetryで変わった 新時代のCloudWatchメトリクス監視
tsukuboshi
0
480
IP重複は"ランディングVPC"で受け止める ― PrivateLink と Transit Gatewayによるマルチテナント接続基盤の設計
hatahata021
3
250
メール到達性を支える、リンク先URLのドメイン評価の扱い方
wind111
0
180
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
42
3.2k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
The World Runs on Bad Software
bkeepers
PRO
72
12k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
960
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
3k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
380
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.2k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
Test your architecture with Archunit
thirion
2
2.5k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
620