-5

ルールは次のようになります。

  • linkhref=を含む最後の行を除くすべての行を削除します。
  • after: href=と before: .cssの内容をhello-worldに置き換えます。
  • ファイル名を引用符、一重引用符、または二重引用符で囲んではいけません

いくつかの例:

これは引用符付きのソース ファイルです。

<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/master.css">

これは新しいソース ファイルです。

<link rel="stylesheet" href="hello-world.css">

これは引用符のないソース ファイルです。

<link rel=stylesheet href=css/reset.css>
<link rel=stylesheet href=css/master.css>

これは新しいソース ファイルです。

<link rel=stylesheet href=hello-world.css>

ファイル名のパスを維持する必要はありません。ただし、その行を記述しているテンプレート言語は括弧またはスペースを使用しない可能性があるため、編集する必要があるものを決定するために <> 括弧またはスペースを使用することはできません。一貫性を保つ唯一のものは、href=[filename].cssです。

私の bash/sed/regex のスキルはひどいですが、これらのツールはおそらくまともな方法で仕事を成し遂げるでしょうか? どうすればこれを行うことができますか?

編集

明確にするために、最終結果は link と href= を含む行の上と下のすべてをそのままにしておきます。ソース ファイルが html ファイルまたはその他のテンプレート ファイルであるとします。

<html>
  <head>
    <title>Hello</title>
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/master.css">
  </head>

  <body><p>...</p></body>
</html>

次のように変更されます。

<html>
  <head>
    <title>Hello</title>
    <link rel="stylesheet" href="css/hello-world.css">
  </head>

  <body><p>...</p></body>
</html>

CSS ファイルのパスも何でもかまいません。

../foo/bar.css
http://www.hello.com/static/css/hi.css
/yep.css
ok.css

新しいファイルのパスは bash スクリプトの引数として提供されるため、正規表現はパスを削除する必要があります。

4

3 に答える 3

3

チャットでの議論に続いて、コマンド ライン スクリプトとして PHP を使用する 1 つのソリューションは次のようになります。

#! /usr/bin/php 
<?php

    $options = getopt("f:r:");
    $inputFile = $options['f'];
    $replacement = $options['r'];
    // read entire contents of input file 
    $inputFileContents = file_get_contents($inputFile);
    // setup the regex and execute the search
    $pattern = '/.*link.*href=["|\']?(.*[\\\|\/]?.*)\.css["|\']?.*/';
    preg_match_all($pattern, $inputFileContents, $matches);
    // remove last occurance of regex 
    // these are the lines we'll want to hang onto
    $matchedLines = $matches[0];
    array_pop($matchedLines);
    // isolate the last css file name
    $matchedFileName = array_pop($matches[1]);
    // first substitution replaces all lines with <link> with 
    // an empty string (deletes them)
    $inputFileContents = str_replace($matchedLines,'',$inputFileContents);
    // second substitution replaces the matched file name
    // with the desired string
    $inputFileContents = str_replace($matchedFileName,$replacement,$inputFileContents);
    //*/
      // save to new file for debugging
      $outputFileName = "output.html";
      $outputFile = fopen($outputFileName,'w+');
      fwrite($outputFile,$inputFileContents);
      fclose($outputFile);
    /*/
      // save changes to original file
      $origFile = fopen($inputFile,'w+');
      fwrite($origFile,$inputFileContents);
      fclose($origFile);
    //*/
    exit();
?>

次のようにコマンドラインからこのスクリプトを実行します-

$ php thisScript.php -f "input.html" -r "hello-world" 
  • -f解析している入力ファイルです。
  • -rcss ファイル名の置換文字列です (この例では "hello-world")。
于 2012-05-06T19:57:08.023 に答える
1

この場合、具体的に答えてください。

同じ css ファイルを 2 回インクルードしても、ユーザーが見る限り害はありません。
したがって、css/reset.css と css/master.css の両方を css/hello-world.css に置き換えることができます。

もっと良い方法があるかもしれませんが、これが簡単な方法であることがわかりました。<script>このケースでは特に機能しますが、他のタグを置き換えたい場合は機能しません。

于 2012-10-19T11:05:33.983 に答える
0

css の前にファイルの最初の部分をインクルードしてから、残りのファイルを css の下にインクルードし、真ん中に正しい css 行をエコーし​​ます。

于 2014-05-16T20:24:53.090 に答える