2014-11-20 2 views
1

우리는 img 리소스의 CDN 배포를 사용하도록 기존 웹 사이트를 이동하고 있습니다. 프로젝트 관리자는 각 페이지에서 img src을 6 개의 다른 CDN 하위 도메인 중 하나에 배포해야한다고 요구했습니다. 즉, 다른 도메인으로의 다운로드 요청이 순차적으로가 아니라 동시에 실행되기 때문에 속도가 향상된다는 것입니다. 나는 이것을 설정해야합니다 즉일괄 교체 CDN 하위 도메인

...이 속으로

<p>Argle bargle.</p> 
<img src="http://old-domain.com/_img/image-a.jpg"> 
<img src="http://old-domain.com/_img/image-b.jpg"> 
<img src="http://old-domain.com/_img/image-c.jpg"> 
<img src="http://old-domain.com/_img/image-d.jpg"> 
<img src="http://old-domain.com/_img/image-e.jpg"> 
<img src="http://old-domain.com/_img/image-f.jpg"> 
<img src="http://old-domain.com/_img/image-g.jpg"> 
<img src="http://old-domain.com/_img/image-h.jpg"> 
<p>Whiz bang.</p> 

...

<p>Argle bargle.</p> 
<img src="http://cdn1.cdn.com/_img/image-a.jpg"> 
<img src="http://cdn2.cdn.com/_img/image-b.jpg"> 
<img src="http://cdn3.cdn.com/_img/image-c.jpg"> 
<img src="http://cdn4.cdn.com/_img/image-d.jpg"> 
<img src="http://cdn5.cdn.com/_img/image-e.jpg"> 
<img src="http://cdn6.cdn.com/_img/image-f.jpg"> 
<img src="http://cdn1.cdn.com/_img/image-g.jpg"> 
<img src="http://cdn2.cdn.com/_img/image-h.jpg"> 
<p>Whiz bang.</p> 

나는 업데이트 할 파일의 수백이 있고 그들은 모두 훨씬 더 복잡보다 위의 샘플. CDN이 하나의 도메인 일 경우, TextWrangler를 사용하여 즉시 모든 파일을 일괄 처리합니다. 하지만 대체 문자열을 어떻게 든 직렬화 (또는 무작위로?)해야합니다.

FileMaker Pro를 프로덕션 프론트 엔드 (네비게이션 생성, 메타 태그 지정 등을 자동화하기 위해)로 사용하므로 모든 HTML을 일련 번호로 표시하는 HTML 출력 필드에서 계산을 시도했지만 계산 용 필드에서 할 수없는 for-each 루프가 필요합니다 (FM Pro는 아니고 FM Pro Advanced가 아니기 때문에 사용자 정의 함수를 사용할 수 없습니다).

아무도 AppleScript로 비슷한 것을 할 수 있습니까? 또는 터미널 텍스트 프로세서를 활용할 수 있습니까? 모든 권장 사항을 주시면 감사하겠습니다.

답변

0

실제로 FileMaker에서 HTML을 쓰고 있습니까? 이 경우 Perform AppleScript을 사용하여 HTML을 조작하려는 경우 다음과 같이 할 수 있습니다.

모범 사례 : "계산 된 AppleScript"대신 항상 "기본 AppleScript"를 사용하십시오. AppleScript에서 데이터를 가져오고 나가려면 FileMaker와 AppleScript간에 읽기 및 쓰기를위한 전역 필드 쌍을 만듭니다. 이렇게하면 다른 응용 프로그램과 대화 할 때 AppleScript 오류를 격리하여 나중에 스크립트 단계로 이어지지 않게 할 수 있습니다. (나는 Global 테이블을 만들고 @Global이라는 테이블 어 큐물을 생성하는 것을 선호합니다 .과 AppleScriptOutput 두 테이블을 생성하는 Global 테이블에서 테이블을 생성해야합니다.이 테이블에 레코드를 만들어야합니다. 그렇지 않으면 신비한 "개체를 찾을 수 없습니다"오류가 발생합니다 애플 스크립트에서.)

Set Field [@Global::AppleScriptInput; $myHtml] 
Perform AppleScript [“ 
    -- Read HTML from a global field in FileMaker 
    set html to get data (table "@Global")'s (field "AppleScriptInput") 

    -- Split html on old domain fragment 
    set my text item delimiters to "<img src=\"http://old-domain.com/_img/" 
    set html_fragments to text items of html 

    -- Prefix resource with CDN domain fragment 
    repeat with i from 2 to length of html_fragments 
     set cdn_number to (i - 2) mod 6 + 1 
     set cdn_fragment to "<img src=\"http://cdn" & cdn_number & ".cdn.com/_img/" 
     set item i of html_fragments to cdn_fragment & item i of html_fragments 
    end repeat 

    -- Re-join html fragments 
    set my text item delimiters to "" 
    set html to html_fragments as text 

    -- Write html back to a global field in FileMaker 
    set data (table "@Global")'s (cell "AppleScriptInput") to html 
”] 
Set Variable [$revisedHtml; @Global::AppleScriptOutput] 

위의 스크립트 단계는 당신이 파일 메이커 변수 $html에서 제공되는 입력을 읽고 당신이 $revisedHtml에 제공되는 출력을 저장합니다.

Set Variable [$html; "<p>Argle bargle.</p> 
    <img src=\"http://old-domain.com/_img/image-a.jpg\"> 
    <img src=\"http://old-domain.com/_img/image-b.jpg\"> 
    <img src=\"http://old-domain.com/_img/image-c.jpg\"> 
    <img src=\"http://old-domain.com/_img/image-d.jpg\"> 
    <img src=\"http://old-domain.com/_img/image-e.jpg\"> 
    <img src=\"http://old-domain.com/_img/image-f.jpg\"> 
    <img src=\"http://old-domain.com/_img/image-g.jpg\"> 
    <img src=\"http://old-domain.com/_img/image-h.jpg\"> 
    <p>Whiz bang.</p>" 
    ] 
Set Variable [$oldDomain; "http://old-domain.com/_img/"] 
Set Variable [$itemCount; PatternCount ($html ; $oldDomain)] 
Loop 
    Exit Loop If [Let ($i = $i + 1 ; If ($i > $itemCount ; Let ($i = "" ; True)))] 
    Set Variable [$html; 
     Let ([ 
      domainPosition = Position ($html ; "http://old-domain.com/_img/" ; 1 ; 1); 
      domainLength = Length ("http://old-domain.com/_img/"); 
      cdnNumber = Mod ($i - 1 ; 6) + 1 
     ] ; 
      Replace ($html ; domainPosition ; domainLength ; "http://cdn" & cdnNumber & ".cdn.com/_img/") 
     ) 
    ] 
End Loop 
Show Custom Dialog ["Result"; $html] 
+0

환상적이고 많은 많은 감사합니다! 각 HTML 페이지는 FMP 레코드로, 폴더로 내보내고'do shell script \ "mkdir을 사용하여 디렉토리 구조를 만듭니다[email protected] 테이블을 사용하는 첫 번째 솔루션은 해당 워크 플로우에 대해 작업하기가 어려울 것으로 보입니다. 몇 가지 개조 된 평범한 두 번째 FM 스크립트 솔루션은 꿀벌입니다. 솔루션을 수정하는 사용자는 Replace 문자열의 세그먼트가 Position 및 Length 문자열과 일치하지 않는지 확인하거나 처음 발생한 인스턴스를 재귀 적으로 덮어 씁니다. – divvuk

+0

아! 재귀 적 문제에 도움이되는 약간의 조정은'domainPosition = Position ($ html; "http://old-domain.com/_img/"; 1; 1);'이 줄을 변경하는 것입니다. domainPosition = Position ($ html; "http://old-domain.com/_img/"; ($ i> 1) * (domainPosition + domainLength); 1);'. 그러면 이전 교체 후 바로 각 위치 검색이 시작됩니다. –

+0

스크립트 편집기에서 해당 지점에서 domainPosition 및 domainLength를 사용할 수 없습니다 (아마도 아직 선언되지 않았기 때문일 수 있습니다). 그리고이 두 위치에서 해당 정의를 하드 코딩하면 모든 바꾸기 문자열이 맨 위에 쓰여집니다. 파일. 그러나 나는 그 문제의 해결책을 요구하지 않고있다. 원래 솔루션에 대한 제 해결 방법이 효과적입니다. 다시 한 번 감사드립니다. – divvuk

0

난 당신이 그렇게 간단한 bash는 스크립트를 사용하는 것이 좋습니다 : 그것은 /home/of/your/files/에 대한 모든 .html 파일에 cdn1.cdn.com/_imgold-domain.com/_img을 대체

#!/bin/bash 

OLD="old-domain.com\/_img" 
NEW="cdn1.cdn.com\/_img" 
DPATH="/home/of/your/files/*.html" 

for f in $DPATH 
do 
    sed "s/$OLD/$NEW/g" "$f" > temp; mv temp "$f"; 
done 

.

+0

감사합니다,하지만 난 각 파일에 걸쳐 여섯 개 가지 영역의 시리즈를 교체해야하고, 나는이 그렇게하지 생각하지 않는다 :

당신은 물론 순수 파일 메이커 스크립트에서 같은 일을 할 수 있습니다. – divvuk

관련 문제