-5

The rules would be:

  • Delete all lines except the last line which contains: link and href=
  • Replace the contents of whatever is after: href= and before: .css with: hello-world
  • Must maintain no quotes, single quotes or double quotes around the file name

A few examples:

This is a source file with quotes:

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

This is the new source file:

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

This is a source file without quotes:

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

This is the new source file:

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

It does not need to maintain the path of the file name. It however cannot use <> brackets or spaces to determine what needs to be edited because the template language which is writing that line might not use brackets or spaces. The only thing that would remain consistent is href=[filename].css.

My bash/sed/regex skills are awful but those tools seem like they will probably get the job done in a decent way? How would I go about doing this?

EDIT

To clarify, the end result would leave everything above and below the lines that contain link and href= alone. Imagine that the source file was an html file or any other template file like so:

<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>

It would be changed to:

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

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

The path of the CSS files might be anything too.

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

The new file's path would be supplied as an argument of the bash script so the regex should remove the path.

4

3 回答 3

3

聊天讨论之后,使用 PHP 作为命令行脚本的一种解决方案如下所示 -

#! /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是我们正在解析的输入文件。
  • -r是 css 文件名的替换字符串(在本例中为“hello-world”)。
于 2012-05-06T19:57:08.023 回答
1

具体回答,对于这种情况:

如果您包含相同的 css 文件两次,就用户而言,它不会造成任何伤害。
因此,您可以将 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 回答