How to Fix Vimeo URLs (or Almost Anything) in Flare Output

This forum is for all Flare related Tips and Tricks.
Have a tip or trick you use while working in Flare? Share it here.
Post Reply
Alan at jjj
Jr. Propeller Head
Posts: 7
Joined: Mon Jan 24, 2022 4:49 am

How to Fix Vimeo URLs (or Almost Anything) in Flare Output

Post by Alan at jjj »

I've previously published this information in the Web-based Outputs forum, but I thought I'd repost here as my solution can be adapted to be more versatile and do many more fixes/work-arounds and general admin with the Flare output.

Firstly, a recap of the purpose of my original post: if you link to private Vimeo videos, then you may have found that Flare versions 2021r3 & 2022 corrupts the Vimeo video URLs in the generated output. For example, the following link:

Code: Select all

src="https://player.vimeo.com/video/646085504?h=ae2e02edaj"
might be rendered by Flare as:

Code: Select all

src="https://player.vimeo.com/video/646085504?"
As you can see, the Vimeo hash code has been removed, and without it the private video will not play. Madcap are aware of this bug. (Please note that all example hash codes shown are not genuine.)

My script shown below will repair the corrupted URL.


At my company the real version of this script was already in place when we discovered the Vimeo URL issue. I simply appended an extra section to deal with the videos. For this forum posting, however, I have cut it down to just show the Vimeo fix, and in this cut down state it has been successfully tested in our environment.

FYI, the full list of jobs it does in our Flare HTML5 Side Navigation output projects is:

1. Insert extra text and buttons in the top bar (between the logo and the search box from the skin).
2. Count the number of topics in the project, and insert this number in the About Page.
3. Extract file tags from each output file's source file and store this data in an XML file for our user access security system.
4. Fix corrupted Vimeo video URLs.

So you can imagine that just about any processing that your HTML output may require could be appended to the list of jobs that the script performs.


The mechanism used is Powershell scripting initiated by the Post-Build Events in a Flare Target. However, you do not need to be familiar with Powershell to use this script because the list of Vimeo video codes to be fixed are entered in a Flare VariableSet formatted as shown below.

In your project, add the following VariableSet named VimeoCodes, replacing these example codes with your own genuine codes:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<CatapultVariableSet>
  <Variable
    Name="AddUser"
    Comment="note: 3 x fake hash codes"
    EvaluatedDefinition="646085504?h=ae2e02edaj">646085504?h=ae2e02edaj</Variable>
  <Variable
    Name="EditUser"
    Comment=""
    EvaluatedDefinition="646070519?h=f0b3d9c4fj">646070519?h=f0b3d9c4fj</Variable>
  <Variable
    Name="Video3"
    Comment=""
    EvaluatedDefinition="646070879?h=243c069c7j">646070879?h=243c069c7j</Variable>
</CatapultVariableSet>
Extend the VariableSet to include the total number of videos that you have in your project.


In your Target's properties (Build Events|Post-Build Event Command box) enter the following 4 commands:

Code: Select all

set PROJECTDIR=$(ProjectDirectory)
set OUTPUTDIR=$(OutputDirectory)
cd %PROJECTDIR%Content
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File .\Fix-jutVimeoCodes.ps1 *>> .\Fix-jutVimeoCodes.log
Finally, place the following Powershell script as filename Fix-jutVimeoCodes.ps1 in your Content Explorer root directory:

Code: Select all

# Powershell script: Fix-jutVimeoCodes.ps1 by alanatjjj@gmail.com 23-JUN-2022. Fixes Vimeo URLs broken by Flare. USE AT YOUR OWN RISK.
#
# Notes/Assumptions:
#  1. Only tested in Windows 10, PowerShell 5.1, Flare 2021r3 & 2022.
#  2. Assumes HTML output is generated in the Output "Content" subdirectory - see Target|Advanced|Output Options.
#  3. Edits all .htm files (not .html files) even if there are no changes, and preserves the original versions in .bak files.
#  4. Fixes only the Vimeo video code + hash code pairs stored in the "VimeoCodes" Flare VariableSet file in the same project.
#  5. The example video code + hash code pairs are not genuine videos. Ensure you insert your own genuine codes.

#UTILITIES-----------------------

function Test-jutFound ($index) {
#returns True if key found (ie. $index is > -1) for more readable code.
#for use, for example, with the index from a IndexOf or LastIndexOf string method for finding a key.
  Return (-1 -lt $index)
}

function Test-jutDirExists ($dir) {
#wrapper to simplify usage to determine if directory exists
  Return (Test-Path -Path $dir -PathType Container)
}

function Test-jutFileExists ($file) {
#wrapper to simplify usage to determine if file exists
  Return (Test-Path -Path $file -PathType Leaf)
}

function Set-jutConstant ($name, $value, $description) {
#simplifies definition of a constant value
  if ( Test-jutFileExists "Variable:$name" ) {   #can also test for variables by using "Variable:" 'drive'
    Remove-Variable -Name $name -Scope Global -Force
  }
  Set-Variable -Name $name -Value "$value" -Option ReadOnly -Scope Global -Description "$description"   #must be ReadOnly instead of Constant so it can be recreated
}

#CONSTANTS-----------------------

#Characters   
Set-jutConstant 'BACKSLASH'         '\'   'Character: backslash'
Set-jutConstant 'COMMA'             ','   'Character: comma'
Set-jutConstant 'DOT'               '.'   'Character: dot'
Set-jutConstant 'QUOTE'             '"'   'Character: quote'
Set-jutConstant 'SLASH'             '/'   'Character: slash'
Set-jutConstant 'UNDERSCORE'        '_'   'Character: underscore'
Set-jutConstant 'AMPERSAND'         '&'   'Character: ampersand'
Set-jutConstant 'ENTITYAMP'         '&'   'Character: ampersand HTML entity'

#filenames & partial filenames
Set-jutConstant 'OUTPUTDIR'         $Env:OUTPUTDIR.TrimEnd($BACKSLASH)   'Absolute pathname of directory where Flare builds the output files'   #also ensures that $OUTPUTDIR doesn't end in a "\" - the Powershell properties convention
Set-jutConstant 'PARTPATHCONTENTNS' '\Content'                           'Partial pathname: Content with no final backslash'
Set-jutConstant 'CONTENTDIR'        ($OUTPUTDIR + $PARTPATHCONTENTNS)    'The Content dir in $OUTPUTDIR'
Set-jutConstant 'EXTBAK'            '.bak'                               'Extension for a general purpose backup/temporary file'
Set-jutConstant 'VIDEOSFILE'        ($Env:PROJECTDIR + 'Project\VariableSets\VimeoCodes.flvar')   'File of Flare variables containing Vimeo private video codes (partial URLs)'
Set-jutConstant 'MIDVIMEOCODE'      '?h='                                'Delimiter midway through Vimeo video code - eg. within full code: "646085504?h=ae2e02eda1"'

#messaging
Set-jutConstant 'OUTWIDTH'          1000                       'Width of lines written to console / log file - prevents wrapping'
Set-jutConstant 'DATEFORMAT'        'dd/MM/yyyy HH:mm'         'Message datetime format'
Set-jutConstant 'WARN'              '***WARNING:'              'Message prefix: Warning'
Set-jutConstant 'ERR'               '***ERROR:'                'Message prefix: Error'



#console messages
'.', '.', '.', '====Fix-jutVimeoCodes.ps1=================================================================================================================='
'Project directory env var   :  >{0}<  ' -f   $Env:PROJECTDIR   #from Flare Target Post Build commands as passed by $(ProjectDirectory) variable - note includes a final "\"
'Output  directory env var   :  >{0}<  ' -f   $Env:OUTPUTDIR    #from Flare Target Post Build commands as passed by $(OutputDirectory)  variable - note includes a final "\"
'Output  directory processing:  >{0}<  ' -f   $OUTPUTDIR        #note that any final "\" has been removed

$now = Get-Date -Format $DATEFORMAT

'', "Post build processing $now"



#MAIN-------------------------------------------------------------------------------------------------------------------------
#Flare [v2021r3] breaks the Vimeo hash code embedded in the URLs (Madcap bug #172869). This code repairs the URLs.
# Rev 2b: now detects both terminators of the truncated URL - '"' if Vimeo player has all the default settings, and '&' if Vimeo player has one or more non-default settings. ie:
#  eg1. corrects from: <iframe class="vimeo-player_0" src="https://player.vimeo.com/video/646085504?&byline=0&title=0&portrait=0" frameborder="0" allowfullscreen="1" width="640px" height="323px"></iframe>
#                  to: <iframe class="vimeo-player_0" src="https://player.vimeo.com/video/646085504?h=ae2e02eda1&byline=0&title=0&portrait=0" frameborder="0" allowfullscreen="1" width="640px" height="323px"></iframe>
#    repair is here---------------------------------------------------------------------------------^^^^^^^^^^^^
#  eg2. corrects from: <iframe class="vimeo-player_0" src="https://player.vimeo.com/video/646085504?" frameborder="0" allowfullscreen="1" width="560px" height="315px"></iframe>
#                  to: <iframe class="vimeo-player_0" src="https://player.vimeo.com/video/646085504?h=ae2e02eda1" frameborder="0" allowfullscreen="1" width="560px" height="315px"></iframe>
#    repair is here---------------------------------------------------------------------------------^^^^^^^^^^^^
$videos = $null

#console messages
'', '', "Repairing Vimeo video URLs in ""$CONTENTDIR"""

if ( ! (Test-jutFileExists $VIDEOSFILE) ) {

  "$ERR Unable to fix broken Vimeo URLs as can't find file of codes: $VIDEOSFILE"

} else {
   
  #obtain the correct set of vimeo codes from Flare's variable set file
  $videos = Select-Xml -Path $VIDEOSFILE -XPath "/CatapultVariableSet/Variable[contains(., `"$MIDVIMEOCODE`")]"
#debug:  $videos.Node | Format-List



  #for every .htm file in output dir
  Get-ChildItem -Path $CONTENTDIR -Recurse -Attributes !Directory -Filter *.htm | ForEach-Object {

    $htm = $_.FullName
    $bak = $htm + $EXTBAK

    #preserve original file
    if ( Test-jutFileExists $htm ) {
      if ( Test-jutFileExists $bak ) {
        Remove-Item -Path $bak -Force
      }
      Rename-Item -Path $htm -NewName $bak -Force
    }

    " processing: $htm"
    $changes = $null;



    #for every line in file
    (Get-Content -Path $bak) | ForEach-Object {

      $line = $_;
       
       
       
      #for every vimeo code
      $videos | ForEach-Object {
      
        $video = $_
        $varname =  $video.Node.Name
        $goodcode = $video.Node.InnerXml
        $badcode =  $goodcode.Substring(0, ($goodcode.IndexOf($MIDVIMEOCODE) + 1))   #this is the corruption caused by Flare - ie. code truncated after the "?" character

        $before = $line;
      
        #set the terminating character(s) for each of the two forms of bad code, then fix
        $term = $ENTITYAMP   #eg: 646085504?& --> 646085504?h=ae2e02eda1&
        $line = $line.Replace(($badcode + $term), ($goodcode + $term))
        $term = $QUOTE       #eg: 646085504?" --> 646085504?h=ae2e02eda1"
        $line = $line.Replace(($badcode + $term), ($goodcode + $term))

        if ( $line -ne $before ) {
          $changes += "   $varname`n";   # "`n" is a newline
          $changes += "$line`n";
        }
    
      }   #end [for every vimeo code]



      $_ = $line;
      $_;
    
    } | Set-Content -Path $htm   #end [for every line in file]; output the processed file that if applicable will now be repaired


       
    #console (log file) info
    if ( $changes ) {
      $changes | Out-String -Width $OUTWIDTH;
    }

  }   #end [for every .htm file in output dir]
 


}

#console message
'', '', 'Post build complete'
When the Target is built and the post-build event commands complete, then your Vimeo URLs will be repaired.

Check the Fix-jutVimeoCodes.log file for details of the repairs. Note that the original versions of the *.htm output files will be retained as *.htm.bak files, so you might want to delete them before publishing your web.



If there are any elements of this script or process that you need assistance with (or indeed, if you need guidance on extending its functionality), I will be happy to help.



Hope you find this useful.
Best wishes
Alan
Post Reply