【问题标题】:How to move comment to new file with awk/sed/perl/etc.?如何使用 awk/sed/perl/etc 将评论移动到新文件?
【发布时间】:2018-04-21 13:00:45
【问题描述】:

我想从 SCSS 文件中提取注释,将它们放在一个新的 markdown 文件中,并从原始文件中删除 cmets。

文件.scss

/***
 * # Typography

 * We use
 * [Roboto](https://fonts.google.com/specimen/Roboto)
 */

/**
 * Body typography:
 * `p`
 *
 *     @example
 *     p This is a paragraph with #[strong bold ]&nbsp;and#[em italic ]&nbsp;styles.</p>
*/
body {
  color: $base-font-color;
}

/**
 * Heading 1:
 * `h1` `.h1`
 */
h1,
.h1 {
  @include h1;
}

预期结果

在流程结束时,我希望:

评论.md

# Typography

* We use
[Roboto](https://fonts.google.com/specimen/Roboto)

 Body typography:
 `p`

     @example
     p This is a paragraph with #[strong bold ]&nbsp;and#[em italic ]&nbsp;styles.</p>

  Heading 1:
 `h1` `.h1`

文件.scss

body {
  color: $base-font-color;
}

h1,
.h1 {
  @include h1;
}

问题

如何使用awk、perl、sed 或其他工具来做到这一点?

【问题讨论】:

    标签: perl awk sed extract


    【解决方案1】:

    我自己实现了一个解决方案

    #!/usr/bin/env bash
    # USAGE
    #   ## Development
    #
    #      # single file
    #      bash ./scripts/extract-md.bash src/base/_typography.scss
    #
    #      # all files
    #      bash ./scripts/extract-md.bash src/base/_typography.scss
    #
    #   ## production (override files)
    #
    #      export PRODUCTION=true
    #      bash ./scripts/extract-md.bash
    
    shopt -s globstar
    
    start() {
      for legacy in $legacies; do
        directory="${legacy%/*}"
        filename="${legacy##*/}"
    
        new_filename="${filename//.scss/.md}"
        documentation="${directory}/${new_filename}"
        extract_comment "$legacy" "$documentation"
    
        stylesheet="${1//.scss/.new.scss}"
        extract_code "$legacy" "$stylesheet"
        [[ $PRODUCTION ]] && mv "$stylesheet" "$legacy"
      done
    }
    
    extract_comment() {
      local legacy="$1"
      local documentation="$2"
      awk '/^\/\*/{flag=1} flag; /^\s?\*\//{print;flag=0}' "$legacy" \
      | perl -p -e 's/\/[\*]+//g' \
      | perl -p -e 's/^\s*[\*]?[ \/]?//g' \
      > "$documentation"
    }
    
    extract_code() {
      local legacy="$1"
      local stylesheet="$2"
      awk 'BEGIN{flag=1}/^\/\*/{flag=0}; /^\s?\*\//{flag=1; next}flag' "$legacy" \
      > "$stylesheet"
    }
    
    legacies=${1:-src/**/*.scss}
    start $legacies
    

    【讨论】:

      猜你喜欢
      • 2023-01-24
      • 2022-07-11
      • 2012-09-29
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多