如何从要点脚本创建语言选择包装器?

我有一个用不同语言编写的要点文件,它们都做同样的事情。

因此,我想创建一个类似于Google docs 文档的语言选择选项。

https://img1.sycdn.imooc.com/657a9cd900017c0306270159.jpg

是否可以创建这样一个接受 Gist 脚本标签并如上所示显示的包装类?

嵌入单个文件一样,我尝试了不同的查询命令,例如 <script src="https://gist.github.com/gistid.js?language=python">,但它们都不起作用。


繁星点点滴滴
浏览 90回答 1
1回答

杨__羊羊

这是我最终得到的处理代码。使用一些 CSS + javascript 隐藏和切换逻辑,它会像 google docs 文档一样工作。如果有人用 css 或 js 更新这个答案,我将不胜感激。import requestsfrom bs4 import BeautifulSoupdef render_gist_by_file(gist_id):&nbsp; &nbsp; result = requests.get(f'https://gist.github.com/{gist_id}.js', headers=git_credential)&nbsp; &nbsp; if result.text.startswith("<!DOCTYPE html>"):&nbsp; &nbsp; &nbsp; return None&nbsp; &nbsp; result = result.text&nbsp; &nbsp; result = result.replace("\\/", "/").replace("\\&", "&").replace("\\$", "$").replace("\\<", "<").replace("\\`", "`").replace("\\n", "\n").replace('\\"', '"')&nbsp; &nbsp; result = html.unescape(result)&nbsp; &nbsp; result = result.split("document.write('")[-1][:-3]&nbsp; &nbsp; bs = BeautifulSoup(result, "html.parser")&nbsp; &nbsp; for tag in bs.find_all(class_="gist"):&nbsp; &nbsp; &nbsp; file_box = tag.find(class_="file-box")&nbsp; &nbsp; &nbsp; root = tag.find(class_="file-box")&nbsp; &nbsp; &nbsp; toggle_div = bs.new_tag('div', attrs={"class": "gist-meta"})&nbsp; &nbsp; &nbsp; for i, d in enumerate(tag.find_all(class_="file")):&nbsp; &nbsp; &nbsp; &nbsp; d["class"] = f"file gist-toggle gist-id-{gist_id}"&nbsp; &nbsp; &nbsp; &nbsp; if i != 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file_box.append(d)&nbsp; # combine to first table&nbsp; &nbsp; &nbsp; for d in tag.find_all(class_="gist-meta"):&nbsp; &nbsp; &nbsp; &nbsp; siblings = list(d.next_elements)&nbsp; &nbsp; &nbsp; &nbsp; file_id, file_name = siblings[4].attrs["href"].split("#")[-1], siblings[5]&nbsp; &nbsp; &nbsp; &nbsp; gist.file_names.append(file_name)&nbsp; &nbsp; &nbsp; &nbsp; toggle_a = bs.new_tag('a', attrs={"id": file_id, "class": f"gist-toggle gist-id-{gist_id}", "onclick": f"toggle('gist-id-{gist_id}', '{file_id}')", "style": "padding: 0 18px"})&nbsp; &nbsp; &nbsp; &nbsp; toggle_a.append(file_name)&nbsp; &nbsp; &nbsp; &nbsp; toggle_div.append(toggle_a)&nbsp; &nbsp; &nbsp; &nbsp; d.extract()&nbsp; # remove bottom nav&nbsp; &nbsp; &nbsp; root.insert(0, toggle_div)&nbsp; &nbsp; &nbsp; for d in islice(tag.find_all(class_="gist-file"), 1, None):&nbsp; &nbsp; &nbsp; &nbsp; d.extract()&nbsp; # remove except first&nbsp; &nbsp; gist.html = str(bs)&nbsp; &nbsp; return gist
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript