From e184a06cbe5e470fed049b69bc6cf998867864b7 Mon Sep 17 00:00:00 2001 From: darksun Date: Thu, 16 Aug 2018 12:39:12 +0800 Subject: [PATCH 1/2] translating by lujun9972 --- sources/tech/20180806 A gawk script to convert smart quotes.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sources/tech/20180806 A gawk script to convert smart quotes.md b/sources/tech/20180806 A gawk script to convert smart quotes.md index 209fd4513b..3a6f856a97 100644 --- a/sources/tech/20180806 A gawk script to convert smart quotes.md +++ b/sources/tech/20180806 A gawk script to convert smart quotes.md @@ -1,3 +1,4 @@ +translating by lujun9972 A gawk script to convert smart quotes ====== @@ -227,7 +228,7 @@ via: https://opensource.com/article/18/8/gawk-script-convert-smart-quotes 作者:[Jim Hall][a] 选题:[lujun9972](https://github.com/lujun9972) -译者:[译者ID](https://github.com/译者ID) +译者:[lujun9972](https://github.com/lujun9972) 校对:[校对者ID](https://github.com/校对者ID) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 From 40fea64130764628df43fc90c5cd3a3857194006 Mon Sep 17 00:00:00 2001 From: darksun Date: Thu, 16 Aug 2018 12:41:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=94=B3=E8=AF=B7=E7=BF=BB=E8=AF=91A=20gaw?= =?UTF-8?q?k=20script=20to=20convert=20smart=20quotes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6 A gawk script to convert smart quotes.md | 93 ------------------- 1 file changed, 93 deletions(-) diff --git a/sources/tech/20180806 A gawk script to convert smart quotes.md b/sources/tech/20180806 A gawk script to convert smart quotes.md index 3a6f856a97..8767bfd18f 100644 --- a/sources/tech/20180806 A gawk script to convert smart quotes.md +++ b/sources/tech/20180806 A gawk script to convert smart quotes.md @@ -17,175 +17,96 @@ You can use different methods to convert quotes. Greg Pittman wrote a [Python sc To start, I wrote a simple gawk function to evaluate a single character. If that character is a quote, the function determines if it should output a plain quote or a smart quote. The function looks at the previous character; if the previous character is a space, the function outputs a left smart quote. Otherwise, the function outputs a right smart quote. The script does the same for single quotes. ``` function smartquote (char, prevchar) { -         # print smart quotes depending on the previous character -         # otherwise just print the character as-is - -         if (prevchar ~ /\s/) { -                 # prev char is a space -                 if (char == "'") { -                         printf("‘"); -                 } -                 else if (char == "\"") { -                         printf("“"); -                 } -                 else { -                         printf("%c", char); -                 } -         } -         else { -                 # prev char is not a space -                 if (char == "'") { -                         printf("’"); -                 } -                 else if (char == "\"") { -                         printf("”"); -                 } -                 else { -                         printf("%c", char); -                 } -         } - } - ``` With that function, the body of the gawk script processes the HTML input file character by character. The script prints all text verbatim when inside an HTML tag (for example, ``. Outside any HTML tags, the script uses the `smartquote()` function to print text. The `smartquote()` function does the work of evaluating when to print plain quotes or smart quotes. ``` function smartquote (char, prevchar) { -         ... - } - - BEGIN {htmltag = 0} - - { -         # for each line, scan one letter at a time: - -         linelen = length($0); - -         prev = "\n"; - -         for (i = 1; i <= linelen; i++) { -                 char = substr($0, i, 1); - -                 if (char == "<") { -                         htmltag = 1; -                 } - -                 if (htmltag == 1) { -                         printf("%c", char); -                 } -                 else { -                         smartquote(char, prev); -                         prev = char; -                 } - -                 if (char == ">") { -                         htmltag = 0; -                 } -         } - -         # add trailing newline at end of each line -         printf ("\n"); - } - ``` Here's an example: ``` gawk -f quotes.awk test.html > test2.html - ``` Sample input: ``` - - -   Test page -   -   -   - - -  

Website logo

-  

"Hi there!"

-  

It's and its.

- - ``` @@ -193,33 +114,19 @@ Sample input: Sample output: ``` - - -   Test page -   -   -   - - -  

Website logo

-  

“Hi there!”

-  

It’s and its.

- - - ``` --------------------------------------------------------------------------------