Skip to content

Instantly share code, notes, and snippets.

@PadreSVK
Created May 14, 2019 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PadreSVK/20504c9f6f99b5ccc4f672688d662558 to your computer and use it in GitHub Desktop.
Save PadreSVK/20504c9f6f99b5ccc4f672688d662558 to your computer and use it in GitHub Desktop.

PS script for ordering citations

sample of literatura.tex file:

% Pro sazbu seznamu literatury použijte jednu z následujících možností
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1) Seznam citací definovaný přímo pomocí prostředí literatura / thebibliography

\begin{literatura}{99}

%-------
%\bibitem{XXXX}
%AAA. \emph{name}\/ [online].[cit.\,14.\,5.\,2019]
%Dostupné z~URL:
%\(<\)\url{url}\(>\).

%-------
%\bibitem{XXXX}
%AAA. \emph{name}\/ [online].[cit.\,14.\,5.\,2019]
%Dostupné z~URL:
%\(<\)\url{url}\(>\).

%-------
%\bibitem{XXXX}
%AAA. \emph{name}\/ [online].[cit.\,14.\,5.\,2019]
%Dostupné z~URL:
%\(<\)\url{url}\(>\).
\end{literatura}
$input ="$PSScriptRoot\literatura.tex"
$output = $input
$content = [System.IO.File]::ReadAllText($input)
$items = $content -split "%-------"
$last = $items[$items.Count-1]
$first = $items[0]
$items = $items[1..($items.Count-2)] #skip first and last item in input file
Clear-Content $output
$itemsToOrder = @()
foreach($item in $items ){
$name = $($item -split [Environment]::NewLine)[2] #name is on index 1
$item = $item.Trim()
$itemToOrder = [pscustomobject]@{
name = $name
item = $("%-------" + [Environment]::NewLine + $item+[Environment]::NewLine)
}
$itemsToOrder+= $itemToOrder
}
$ordered = $itemsToOrder | Sort-Object -Property name
Write-Host "ordered $($ordered.Count) citations"
Add-Content $output -Value $first -Encoding UTF8
$ordered | Select-Object -Property item | %{ Add-Content $output -Value $_.item -Encoding UTF8}
Add-Content $output -Value "%-------" -Encoding UTF8
Add-Content $output -Value $last -Encoding UTF8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment