Pgml convert improvements revised - #1482
Open
drgrice1 wants to merge 15 commits into
Open
Conversation
block. Also, removes empty macros and duplicate macros. In addition, if it appears that the problem is already in PGML mode, then return the file without changes.
Also, return a hash of the converted code and any errors.
Add better documentation of the convert-to-pgml script. Separate the loadMacros to a separate subroutine and perform error handling in the subroutine to better capture possible syntax errors. Also, add a better way to parse the answer blanks in PGML form.
block. Also, removes empty macros and duplicate macros. In addition, if it appears that the problem is already in PGML mode, then return the file without changes.
Also, return a hash of the converted code and any errors.
Add better documentation of the convert-to-pgml script. Separate the loadMacros to a separate subroutine and perform error handling in the subroutine to better capture possible syntax errors. Also, add a better way to parse the answer blanks in PGML form.
And rewrite the list of macros to be removed from the list.
The loadMacros parsing is now much more versatile. In addition to basic single and double quoting, and `qw` quotes, it can handle any quotelike quotes. There is a minimal attempt to keep comments inside of a `loadMacros` call. Basically, if a macro is removed from the list, then a comment after it is also removed. If a macro is kept, then so is the comment that was after it. If there is a line that only has a comment on it, then if the preceding line had a comment it is assumed that the comment continues that one, and if that is kept so is the next line. Otherwise the comment line is dropped. Previously the `MathObjects.pl` macro was removed if loaded, since it is loaded by `PGML.pl`. Now `niceTables.pl` is also removed, since `PGML.pl` also loads that. The naive approach of replacing `ans_rule` calls with PGML has been removed. Instead any `ans_rule` call is just wrapped in `[@ ... @]*`, and the corresponding `ANS` calls are left alone. One advantage of this approach is that after the conversion is completed, the problem actually works. In addition, the previous approach was clobbering the `ans_rule` calls in many cases that give information about what needs to be done to properly convert the answers into the PGML answer syntax. The con is that the conversion into PGML syntax must be done manually. However, since that really was the case before other than basically just putting a non-functional PGML answer template into place, that is not really a tradeoff. Instead of handling only two very specific forms of heredoc/nowdoc syntax `SOLUTION(EV3(<<'END_SOLUTION'));` and `TEXT(EV2(<<EOT))` where the heredoc/nowdoc terminator was fixed and must be exactly what is shown above, any such form is handled. The heredoc/nowdoc terminator can be anything the author specified and an unquoted, singled quoted, or double quoted terminator can be used. In addition both `EV2` and `EV3` are covered, and the `HINT` form is also handled. In addition spaces can be in those constructs as allowed by Perl. Note that there are OPL problems that do all of those things, so it was really quite naive to only literally cover those two cases. Variable interpolation now actually works much better, including for hash and array access, and hash reference and array reference access. Note that a file that already uses PGML and does not have any of the old style PG constructs is actually completely left alone. @pstaapb's openwebwork#1425 still modifies those files in some cases. The `bin/convert-to-pgml.pl` script no longer accepts `-s|--suffix` argument. Instead it accepts a `-e|--extension-prefix` argument. That is much the same except that its value is prepended to `.pg` instead of used as the extension itself. It defaults to `pgml` as the `suffix` option did before. So if `problem.pg` is converted, then the result will be written to `problem.pgml.pg`. Thus the resulting file has the correct pg problem file extension. A new option for the `bin/convert-to-pgml.pl`, `-p|--output-path` was added. When given, the converted file is written into that directory with the same name as the original file. The `-b|--backup` and `-e|--extension-prefix` options are ignored, unless the output path resolves to the directory containing the original file, in which case those options will still apply. TRhere were some other minor things fixed along the way as well that I did not list here, such as an issue with the conversion of the `$HR` variable into a PGML `---` in which the `---` was added, but the `$HR` variable left and changed into `[$HR]` resulting in ``` --- [$HR] ``` in the converted file. This was done in collaboration with Claude.
drgrice1
force-pushed
the
pgml-convert-improvements-revised
branch
from
July 28, 2026 12:23
9b09961 to
43bffb9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The loadMacros parsing is now much more versatile. In addition to basic single and double quoting, and
qwquotes, it can handle any quotelike quotes. There is a minimal attempt to keep comments inside of aloadMacroscall. Basically, if a macro is removed from the list, then a comment after it is also removed. If a macro is kept, then so is the comment that was after it. If there is a line that only has a comment on it, then if the preceding line had a comment it is assumed that the comment continues that one, and if that is kept so is the next line. Otherwise the comment line is dropped.Previously the
MathObjects.plmacro was removed if loaded, since it is loaded byPGML.pl. NowniceTables.plis also removed, sincePGML.plalso loads that.The naive approach of replacing
ans_rulecalls with PGML has been removed. Instead anyans_rulecall is just wrapped in[@ ... @]*, and the correspondingANScalls are left alone. One advantage of this approach is that after the conversion is completed, the problem actually works. In addition, the previous approach was clobbering theans_rulecalls in many cases that give information about what needs to be done to properly convert the answers into the PGML answer syntax. The con is that the conversion into PGML syntax must be done manually. However, since that really was the case before other than basically just putting a non-functional PGML answer template into place, that is not really a tradeoff.Instead of handling only two very specific forms of heredoc/nowdoc syntax
SOLUTION(EV3(<<'END_SOLUTION'));andTEXT(EV2(<<EOT))where the heredoc/nowdoc terminator was fixed and must be exactly what is shown above, any such form is handled. The heredoc/nowdoc terminator can be anything the author specified and an unquoted, singled quoted, or double quoted terminator can be used. In addition bothEV2andEV3are covered, and theHINTform is also handled. In addition spaces can be in those constructs as allowed by Perl. Note that there are OPL problems that do all of those things, so it was really quite naive to only literally cover those two cases.Variable interpolation now actually works much better, including for hash and array access, and hash reference and array reference access.
Note that a file that already uses PGML and does not have any of the old style PG constructs is actually completely left alone. @pstaapb's #1425 still modifies those files in some cases.
The
bin/convert-to-pgml.plscript no longer accepts-s|--suffixargument. Instead it accepts a-e|--extension-prefixargument. That is much the same except that its value is prepended to.pginstead of used as the extension itself. It defaults topgmlas thesuffixoption did before. So ifproblem.pgis converted, then the result will be written toproblem.pgml.pg. Thus the resulting file has the correct pg problem file extension.A new option for the
bin/convert-to-pgml.pl,-p|--output-pathwas added. When given, the converted file is written into that directory with the same name as the original file. The-b|--backupand-e|--extension-prefixoptions are ignored, unless the output path resolves to the directory containing the original file, in which case those options will still apply.TRhere were some other minor things fixed along the way as well that I did not list here, such as an issue with the conversion of the
$HRvariable into a PGML---in which the---was added, but the$HRvariable left and changed into[$HR]resulting inin the converted file.
Note that this is built on top of #1425, and is intended to replace that pull request.
This was done in collaboration with Claude.