Loffice: Create empty LibreOffice file if file does not exist.

This commit is contained in:
Ole Tange 2020-10-15 16:30:45 +02:00
parent dbf36dfd17
commit 4c03edadce
3 changed files with 82 additions and 185 deletions

76
Loffice/Loffice Executable file
View file

@ -0,0 +1,76 @@
#!/usr/bin/perl
# If argument does not exist: Create a new file of the given type
use File::Temp qw(tempfile);
my @files = grep { not /^-/ and not -e $_ } @ARGV;
my $gen = {
"fodt" => odt,
"odt" => odt,
"fods" => ods,
"ods" => ods,
"fodp" => odp,
"odp" => odp,
"fodd" => odd,
"odd" => odd,
};
for(@files) {
my $extension = (/\.([^.]+)/)[0] || die;
$gen->{$extension}($_,$extension);
}
exec 'loffice', @ARGV;
sub ods {
my $file = shift;
my $extension = shift;
my($tmphandle,$tmpname) = tempfile(TEMPLATE => 'loffXXXX', SUFFIX => ".csv");
system(qw(unoconv -d spreadsheet -f), $extension, '-o', $file, $tmpname);
unlink $tmpname;
}
sub odt {
my $file = shift;
my $extension = shift;
my($tmphandle,$tmpname) = tempfile(TEMPLATE => 'loffXXXX', SUFFIX => ".txt");
system(qw(unoconv -d document -f), $extension, '-o', $file, $tmpname);
unlink $tmpname;
}
sub odp {
my $file = shift;
my $extension = shift;
my($tmphandle,$tmpname) = tempfile(TEMPLATE => 'loffXXXX', SUFFIX => ".svg");
print $tmphandle q{<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg viewBox="0 0 28000 15750"
xmlns:ooo="http://xml.openoffice.org/svg/export">
<defs>
<g id="ooo:meta_slides" ooo:number-of-slides="1" ooo:start-slide-number="0">
</g>
</defs>
</svg>
};
close $tmphandle;
system(qw(unoconv -d presentation -f), $extension, '-o', $file, $tmpname);
unlink $tmpname;
}
sub odd {
my $file = shift;
my $extension = shift;
my($tmphandle,$tmpname) = tempfile(TEMPLATE => 'loffXXXX', SUFFIX => ".svg");
print $tmphandle q{<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg viewBox="0 0 21000 29700">
</svg>
};
close $tmphandle;
system(qw(unoconv -d graphics -f), $extension, '-o', $file, $tmpname);
unlink $tmpname;
}

View file

@ -1,10 +1,11 @@
CMD = blink 2grep 2search burncpu drac duplicate-packets em emoticons \ CMD = blink 2grep 2search burncpu drac duplicate-packets em emoticons \
encdir field find-first-fail forever fxkill G gitnext gitundo \ encdir field find-first-fail forever fxkill G gitnext gitundo \
goodpasswd histogram mtrr mirrorpdf neno off parsort pdfman \ goodpasswd histogram Loffice mtrr mirrorpdf neno off parsort \
pidcmd pidtree plotpipe puniq ramusage rand rclean rina rn \ pdfman pidcmd pidtree plotpipe puniq ramusage rand rclean \
rrm seekmaniac shython sound-reload splitvideo stdout swapout \ rina rn rrm seekmaniac shython sound-reload splitvideo stdout \
T teetime timestamp tracefile transpose upsidedown vid \ swapout T teetime timestamp tracefile transpose upsidedown \
w4it-for-port-open whitehash wifi-reload wssh ytv yyyymmdd vid w4it-for-port-open whitehash wifi-reload wssh ytv \
yyyymmdd
all: blink/blink.1 2search/2grep.1 2search/2search.1 \ all: blink/blink.1 2search/2grep.1 2search/2search.1 \
burncpu/burncpu.1 drac/drac.1 encdir/encdir.1 field/field.1 \ burncpu/burncpu.1 drac/drac.1 encdir/encdir.1 field/field.1 \

View file

@ -1,180 +0,0 @@
#!/bin/bash
: <<'=cut'
=encoding utf8
=head1 NAME
binsearch - do binary search to find a numeric limit
=head1 SYNOPSIS
B<binsearch> I<command>
=head1 DESCRIPTION
B<binsearch> runs I<command> with a single number. It returns highest
value that I<command> succeeds for.
=head1 OPTIONS
=over 4
=item B<-2>
Instead of the command a single argument, give the command 2 argument:
I<from> I<to>.
=item B<-q>
Quiet. Ignore output from B<command>.
=back
=head1 EXAMPLES
=head2 Find the last file
This is a silly way to find the last non-existing file (namely 244):
touch {245..800}
binsearch ls
This is a silly way to find the last file (namely 800):
touch {1..800}
binsearch ls
=head2 Test a bash function
Test how long an argument /bin/echo can take
. $(which binsearch)
singleecho() {
/bin/echo $(perl -e 'print "x"x'$1) >/dev/null
}
binsearch singleecho
=head2 Test a bash function that takes from and to as arguments
Use a function that takes two arguments
. $(which binsearch)
greplines() {
env | perl -ne "$1..$2 and print" | grep HOME=
}
binsearch -2 -q greplines
=head1 AUTHOR
Copyright (C) 2020 Ole Tange,
http://ole.tange.dk and Free Software Foundation, Inc.
=head1 LICENSE
Copyright (C) 2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=head1 SEE ALSO
B<eval>(1)
=cut
binsearch() {
_binsearch() {
low=$1
high=$2
# echo $low-$high
if [ $low -gt $(($high - 2)) ]; then
return
fi
shift
shift
middle=$(( ( $low + $high ) / 2 ))
if _run $low $middle "$@" ; then
low=$middle
else
high=$middle
fi
_binsearch $low $high "$@"
}
_run() {
local a="$1"
local b="$2"
shift
shift
# echo "a=$a b=$b $@"
if $opt2 ; then
eval "$not" "$@ $a $b" "$quiet"
else
eval "$not" "$@ $b" "$quiet"
fi
}
quiet=""
opt2=false
args=$(getopt 'q2' $*) || exit
# now we have the sanitized args... replace the original with it
set -- $args
while true; do
case $1 in
(-2) opt2=true; shift;;
(-q) quiet=">/dev/null 2>/dev/null"; shift;;
(--) shift; break;;
esac
done
# If function(1) = false: run 'not function()' instead
if _run 1 1 "$@" ; then
not=''
else
not='!'
fi
# exponential search for the first value that is false
# low = previous value (function($low) == true)
# high = low * 2 (function($high) == false)
high=1
while _run 1 $high "$@" ; do
low=$high
high=$(( $high*2 ))
if [ $high -gt 4611686018427387900 ] ; then
echo "$0: Error: exit value does not change of '$@'" >&2
return
fi
done
# echo "low: $low high: $high not: $not"
_binsearch $low $high "$@" 2>/dev/null
echo $low
}
if [ -z "$*" ] ; then
# source the bash function
# . $(which binsearch)
true
else
# binsearch command
binsearch "$@"
fi