2017-10-26 5 views
3

を提出します。がマークダウンからHTMLコメントを削除する1は、最終的なHTMLソースに表示されたコメントを防ぐために必要がある場合、例えば、HTMLにマークダウンから変換するときにこれは便利になるでしょう

例入力my.md

# Contract Cancellation 

Dear Contractor X, due to delays in our imports, we would like to ... 
<!-- 
    ... due to a general shortage in the Y market 
    TODO make sure to verify this before we include it here 
--> 
best, 
me <!-- ... or should i be more formal here? --> 

出力例:Linuxの

# Contract Cancellation 

Dear Contractor X, due to delays in our imports, we would like to ... 

best, 
me 

、私はこのようなものだろう:

cat my.md | remove_html_comments > my-filtered.md 

を私も書くことができていますいくつかの一般的なケースを処理するAWKスクリプト 私が理解したように、単純なテキスト操作のための一般的なツール(sedなど)のAWKも他のツールも実際にはこの仕事に依存しません。 HTMLパーサーを使用する必要があります。

は、どのように、どのようなツールを使用して、適切なremove_html_commentsスクリプトを書くには?

+0

別ファイルに個人のメモを書き込む – Justinas

+0

例を少し変更しました。確かに、ドキュメント内のコメントを別々のファイルに入れておくことは利点があります。特に、多数のリビジョンがある場合は、複数の人が共有しています。 – hoijui

+0

どのMarkdownプロセッサを使用していますか? – Chris

答えて

0

あなたは何ができるのvimで開く場合:

:%s/<!--\_.\{-}-->//g 

_と。正規表現を改行文字でもすべての文字に一致させることができます。{ - }はそれを怠惰にするためのものです。そうしないと、最初のコメントから最後のコメントまでのすべての内容が失われます。

私は、sedの上で同じ表現を使用しようとしましたが、それは文句を言わない仕事。

+2

これはawkスクリプトよりもどのように優れていますか? – helb

+0

ここにこれを持たせることはできませんが、私の目的(自動ビルド/生成、手動ステップを追加することは理にかなっていません。 – hoijui

1

それは私がHTMLパーサを使用するビットカウンタ - 直感的な、芽かもしれません。 PythonとBeautifulSoupと

例:

import sys 
from bs4 import BeautifulSoup, Comment 

md_input = sys.stdin.read() 

soup = BeautifulSoup(md_input, "html5lib") 

for element in soup(text=lambda text: isinstance(text, Comment)): 
    element.extract() 

# bs4 wraps the text in <html><head></head><body>…</body></html>, 
# so we need to extract it: 

output = "".join(map(str, soup.find("body").contents)) 

print(output) 

出力:

$ cat my.md | python md.py 
# Contract Cancellation 

Dear Contractor X, due to delays in our imports, we would like to ... 

best, 
me 

それはあなたがた.mdファイルを持っている可能性のある他のHTMLを壊すべきではありません(それは、フォーマット、コードを変更する場合がありますビットではなく、それは意味です):

enter image description here

もちろん、それを使用することを決定した場合は、それをテストしてください。

編集 - ここではそれをオンラインで試してみてください:https://repl.it/NQgG(入力はinput.mdから読み込まれ、標準入力ではない)

1

をこのawkの読みやすくすると説明については

$ awk -v FS="" '{ for(i=1; i<=NF; i++){if($i$(i+1)$(i+2)$(i+3)=="<!--"){i+=4; p=1} else if(!p && $i!="-->"){printf $i} else if($i$(i+1)$(i+2)=="-->") {i+=3; p=0;} } printf RS}' file 
Dear Contractor X, due to delays in our imports, we would like to ... 



best, 
me 

を動作するはずです:

おそらくより簡単に
awk -v FS=""         # Set null as field separator so that each character is treated as a field and it will prevent the formatting as well 
    '{ 
     for(i=1; i<=NF; i++)     # Iterate through each character 
     { 
      if($i$(i+1)$(i+2)$(i+3)=="<!--") # If combination of 4 chars makes a comment start tag 
       {       # then raise flag p and increment i by 4 
        i+=4; p=1     
       } 
      else if(!p && $i!="-->")   # if p==0 then print the character 
       printf $i 
      else if($i$(i+1)$(i+2)=="-->") # if combination of 3 fields forms comment close tag 
       {       # then reset flag and increment i by 3 
        i+=3; p=0; 
       } 

     } 

     printf RS 

     }' file 
0

私のAWKソリューション、少なくとも高レベルの開発者のために、そして@batManのいずれかを理解します。機能はほぼ同じでなければなりません。

ファイルremove_html_comments:私はあなたがほとんどPandocを使用するあなたのコメントから見る

#!/usr/bin/awk -f 
# Removes common, simple cases of HTML comments. 
# 
# Example: 
# > cat my.html | remove_html_comments > my-filtered.html 
# 
# This is usefull for example to pre-parse Markdown before generating 
# an HTML or PDF document, to make sure the commented out content 
# does not end up in the final document, # not even as a comment 
# in source code. 
# 
# Example: 
# > cat my.markdown | remove_html_comments | pandoc -o my-filtered.html 
# 
# Source: hoijui 
# License: CC0 1.0 - https://creativecommons.org/publicdomain/zero/1.0/ 

BEGIN { 
    com_lvl = 0; 
} 

/<!--/ { 
    if (com_lvl == 0) { 
     line = $0 
     sub(/<!--.*/, "", line) 
     printf line 
    } 
    com_lvl = com_lvl + 1 
} 

com_lvl == 0 

/-->/ { 
    if (com_lvl == 1) { 
     line = $0 
     sub(/.*-->/, "", line) 
     print line 
    } 
    com_lvl = com_lvl - 1; 
} 
2

Pandoc version 2.0(2017年10月29日公開、adds a new option --strip-comments)。 related issueはこの変更にいくつかのコンテキストを提供します。

最新バージョンにアップグレードし、--strip-commentsをコマンドに追加すると、変換プロセスの一部としてHTMLコメントが削除されます。

関連する問題