#
# $Id: parse_table.pl 9 2026-03-03 18:33:36Z jlpoole $
# $HeadURL: https://ares/svn/workstation/trunk/perl/Voron/parse_table.pl $
#
# Usage:
#    perl parse_table.pl
# or
#    perl parse_tables.pl >manifest.txt
#
# Parses the HTML provided by the printer of parts and creates
# a manifest of the STL files
# 2 formats selectable by remming code below:
#
#   PATH + FILE NAME [TAB] COUNT
#
#   PATH [TAB] FILE NAME [TAB] SUFFIX
#
# See examples below
#

use strict;
use warnings;

use File::Basename;
use HTML::TableExtract;

my %names;

#
# ick: spaces in file names
#
my $file = '/home/jlpoole/Nextcloud2/Voron/West3D V2.4 LDO Rev D Printed Parts.html';
#
#   <tbody><tr><th>STL Preview</th><th>Filename</th><th>Quantity Needed</th></tr>
#
my $html_string;
{
    local $/;
    open(IN,"$file") or die "Could not open $file";
    $html_string = <IN>;
    close(IN);
}
my $te = HTML::TableExtract->new( headers => ['STL Preview','Filename','Quantity Needed'] );
$te->parse($html_string);
# Examine all matching tables
foreach my $ts ($te->tables) {
  print "Table (", join(',', $ts->coords), "):\n";
  foreach my $row ($ts->rows) {
      #print join(',', @$row), "\n";
      my $path_file = @$row[1];
      my ($name,$path,$ext) = fileparse($path_file, '\..*');
      $names{$name}++;
      print "Warning: duplicate name found: $name\n" if $names{$name} > 1;
      my $quantity = @$row[2];
      $quantity =~ /.*?(\d+).*?/;
      my $count = $1;
      if (defined $count){
	  #
	  # Below is used for creating manifest.txt used by extract_first_path.pl
	  #
	  print "$path_file\t$count\n";
	  #
	  # Below is for QA
	  #
	  #print "$path\t$name\t$ext\n";
      }	 
  }
}

=pod

1) Example output when printing with: "$path\t$name\t$ext\n"

jlpoole@jp ~/workstation/perl/Voron $ perl parse_table.pl 
Table (0,1):
Voron-2/STLs/Test_Prints/       Heatset_Practice        .stl
Voron-2/STLs/Electronics_Bay/   lrs_200_psu_bracket_x2  .stl
Voron-2/STLs/Electronics_Bay/   pcb_din_clip_x3 .stl
Voron-2/STLs/Electronics_Bay/   wago_221-415_mount_3by5 .stl

2) Example output when printing with: "$path_file\t$count\n
jlpoole@jp ~/workstation/perl/Voron $ perl parse_table.pl 
Table (0,1):
Voron-2/STLs/Test_Prints/Heatset_Practice.stl   1
Voron-2/STLs/Electronics_Bay/lrs_200_psu_bracket_x2.stl 2
Voron-2/STLs/Electronics_Bay/pcb_din_clip_x3.stl        3
Voron-2/STLs/Electronics_Bay/wago_221-415_mount_3by5.stl        1

=cut
