Jump to content

User:Michael Bednarek/WTDtHs

From Wikipedia, the free encyclopedia
WTDtHs is a console application

This tool allows the insertion of proper dates as {{Hs|date}} templates into table columns (WTDtHs=Wikipedia Tables Dates as Hidden fields). It requires the Microsoft Windows Script Host.

Instructions

[edit]

Save the code below into a file and name it WTDtHs.WSF.

Save the Wikipedia table code you want to process into a file named as you wish.
Invoke the program with WScript //Nologo WTDtHs.wsf "file.txt" >"output.txt".

The Wikipedia table code must specify each row in a single record. Example[E 1]:

…
| 17 February 1794 ||  ''Saffo ossia I riti d'Apollo Leucadio '' || [[opera seria|dramma per musica]] || 2 acts ||Antonio Simone Sografi || [[Venice]], [[La Fenice]]
|-
| 26 January 1796 || ''La Lodoiska''  ||  dramma per musica || 3 acts ||Francesco Gonella after Claude-François Fillette-Loraux (after a novel by Jean-Baptiste Louvet de Couvray) ||  Venice, La Fenice 
…

would result in

…
|{{Hs|1794-02-17}}17 February 1794 ||  ''Saffo ossia I riti d'Apollo Leucadio '' || [[opera seria|dramma per musica]] || 2 acts ||Antonio Simone Sografi || [[Venice]], [[La Fenice]]
|-
|{{Hs|1796-01-26}}26 January 1796 || ''La Lodoiska''  ||  dramma per musica || 3 acts ||Francesco Gonella after Claude-François Fillette-Loraux (after a novel by Jean-Baptiste Louvet de Couvray) ||  Venice, La Fenice 
…

The program performs no error checking; it is not comprehensively tested but it works for me. Report errors on this page's Talk page. Use at your own risk. Note especially that no check is performed whether the redirected output file already exists and whether you really want to overwrite it. I am unlikely to develop this any further, e.g. dealing with more freely formatted Wikipedia table code where line breaks are used to separate columns.

  1. ^ Example taken from List of operas by Mayr (this is a previous version).

Source code

[edit]
<job id="WTDtHs">
  <runtime>
    <description>
      WTDtHs V1.20 Jan-2009 Michael Bednarek mailto:rot13(zo@zorqanerx.pbz)
      "Insert template {{Hs}} into columns which contain dates"
    </description>
    <unnamed
      name = "File"
      helpstring = "File with Wikipedia table code to process where the first line contains the column number to convert."
      many = "False"
      required = "1"
     />
     <example>
     Example: WScript //Nologo WTDtHs.wsf "file.txt"
     "Note: //Nologo is recommended because output is written to stdout"

     "To redirect output to a file:"
     Example: WScript //Nologo WTDtHs.wsf "file.txt" >"output.txt"
     </example>
  </runtime>

<script language="VBScript">

Option Explicit

' Arguments:
' 1: File to be processed
'    Wikipedia table code where each row is contained in one record.
'    Example:
'  1
'  {|class="wikitable sortable"
'  !width="10%"|Premiere!!width="23%"|Title!!width="12%"|Genre!!width="3%"|Length!!width="22%"|Libretto!!width="30%"|Venue
'  |-
'  |17 February 1794||''Saffo ossia I riti d'Apollo Leucadio ''||[[opera seria|dramma per musica]]||2 acts||{{Hs|Sografi}}Antonio Simone Sografi||[[Venice]], [[La Fenice]]
'  |-
'  |26 January 1796||{{Hs|Lodoiska1}}''La Lodoiska''||dramma per musica||3 acts||{{Hs|Gonella}}Francesco Gonella after Claude-François Fillette-Loraux (after a novel by Jean-Baptiste Louvet de Couvray)||Venice, La Fenice
'  …
'    would result in
'  {|class="wikitable sortable"
'  !width="10%"|Premiere!!width="23%"|Title!!width="12%"|Genre!!width="3%"|Length!!width="22%"|Libretto!!width="30%"|Venue
'  |-
'  |{{Hs|1794-02-17}}17 February 1794||''Saffo ossia I riti d'Apollo Leucadio ''||[[opera seria|dramma per musica]]||2 acts||{{Hs|Sografi}}Antonio Simone Sografi||[[Venice]], [[La Fenice]]
'  |-
'  |{{Hs|1796-01-26}}26 January 1796||{{Hs|Lodoiska1}}''La Lodoiska''||dramma per musica||3 acts||{{Hs|Gonella}}Francesco Gonella after Claude-François Fillette-Loraux (after a novel by Jean-Baptiste Louvet de Couvray)||Venice, La Fenice
'  …

' Version history:
' V1.00 Dec-2008 Original version
' V1.10 Jan-2009 Fixed 1st column
' V1.11 Jan-2009 Fixed bug for the "|-" separator lines
' V1.20 jan-2009 It turns out that some fields give IsDate() = True for certain non-date fields,
'                notably Telemann's TWV numbers (nn:nn). So we now expect a first record which contains
'                the number of the column to convert.
' To do: Check whether a column has already a template {{Hs}}, otherwise this can't be run more than once.

Dim objArgs   ' As WshArguments
Dim objStream ' As TextStream
Dim StdOut    ' As Object ' of StandardStreamTypes

Dim lngDoCol  ' As Long       ' Column number to convert
Dim strLine   ' As Variant    ' Array
Dim k         ' As Long       ' How many columns
'Dim i         ' As Long       ' Loop through columns
Dim str1      ' As String     ' 1st character of 1st column
Const ForReading = 1
Const TristateUseDefault = -2

Set objArgs = WScript.Arguments.Unnamed ' Obtain command line arguments

With objArgs
  If .Count <> 1 Then ' Must have exactly one command line argument
    WScript.Arguments.ShowUsage
    WScript.Quit(8)
  Else
    Set StdOut = WScript.StdOut
    Set objStream = WScript.CreateObject("Scripting.FileSystemObject").OpenTextFile(.Item(0), ForReading, vbFalse, TristateUseDefault)
    lngDoCol = objStream.ReadLine - 1 ' First record, containing column number to convert (zero-based)
    Do While Not objStream.AtEndOfStream
      strLine = Split(objStream.ReadLine, "||")
      str1 = ""
      k = UBound(strLine)
      If k > 0 Then    ' Any columns?
        If Left(strLine(0),1) = "|" Then ' Special case: 1st column
          str1 = "|"
          strLine(0) = Mid(strLine(0),2)
        End If
        'For i = 0 To k ' loop through the columns
           If IsDate(strLine(lngDoCol)) Then
             strLine(lngDoCol) = "{{Hs|" & PadLeft(Year(strLine(lngDoCol)),4,"0") & "-" & PadLeft(Month(strLine(lngDoCol)),2,"0") & "-" & PadLeft(Day(strLine(lngDoCol)),2,"0") & "}}" & strLine(lngDoCol)
           Else
             strLine(lngDoCol) = "{{Hs|9999-99-99}}" & strLine(lngDoCol) ' Unknown date (not IsDate() = True)
           End If
        'Next ' i (column)
      End If  ' Any columns?
      StdOut.WriteLine str1 & Join(strLine, "||")
    Loop
    objStream.Close
    StdOut.Close
  End If
End With

Set objStream = Nothing
Set StdOut = Nothing
Set objArgs = Nothing
WScript.Quit (0)

Function PadLeft(lngLong, lngLength, strPad)
  If Len(lngLong) < lngLength Then
    PadLeft = String(lngLength - Len(lngLong), strPad) & lngLong
  Else
    PadLeft = lngLong
  End If
End Function

</script>
</job>

Licence

[edit]

See also

[edit]