Ooops
November 25, 2020, 06:13:41 AM

Author Topic: [bb] BB Code Formatter by Alaric [ 1+ years ago ]  (Read 719 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] BB Code Formatter by Alaric [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : BB Code Formatter
Author : Alaric
Posted : 1+ years ago

Description : Lately I've been realizing what a neccessity the abillity to read and get information from others source code can be.  However, one of my biggest pet peeves is people that do not format their code like I do.  Eventually I got tired of reformatting it into a readable format so I wrote this little program to do it for me :).  It still has a couple of bugs around comments and the header declaration (global, dim, etc.) and setup (Graphics, apptitle, seedrnd, etc.) commands, but I plan on working those out soon.  The code for the program is self-formatted and makes a good preview.  I hope that it helps make someone's life a little less boring!

Note:  The space at the end of the code (50 blank lines) is not a glitch.  If you don't like it take it out, but I like to be able to scroll all the way past my code.


Code :
Code: BlitzBasic
  1. ;Average Commands
  2. Global pluscoms$=Lower$(";if;select;repeat;for;while;function;type;")
  3. Global mincoms$=Lower$(";until;forever;next;wend;endif;end;")
  4. Global backcoms$=Lower$(";then;case;default;elseif;else;")
  5. Global comends$=Chr$(34)+Chr$(9)+" ~!@#$%^&*()_+|`-={}:<>?[];',./"
  6.  
  7. ;Header commands
  8. Const headercoms$=";graphics;apptitle;automidhandle;seedrnd;setbuffer;"
  9. Const constcoms$=";const;"
  10. Const globalcoms$=";global;"
  11. Const localcoms$=";local;"
  12. Const arraycoms$=";dim;"
  13.  
  14. ;Globalize some assorted variables
  15. Global hitfuncs=0
  16. Global needspace=0
  17. Global LastComSet=0 ;Used to see which header command set (declaration or such) was used
  18. Global crlf$=Chr$(10)+Chr$(13)
  19.  
  20. ;Select the files
  21. Global fileloc$=RequestFile$("Select a File to reformat")
  22. If Not FileType(fileloc$) Then Notify("Could not open file!"):End
  23. Global filein=ReadFile(fileloc)
  24. Global fileout=WriteFile(parseoutext$(fileloc)+"_clean"+parseext$(fileloc))
  25.  
  26. ;Begin the Main Loop
  27. While Not Eof(filein)
  28.         linefeed$=ReadLine(filein)
  29.         linefeed=Trim$(Replace$(linefeed,Chr$(9),""))
  30.         If Trim$(linefeed) <> "" Then
  31.                 If Left$(linefeed,1)=";" Then WriteLine(fileout,"")
  32.                 newtab%=needstab%(linefeed)
  33.                 cleanheader(linefeed)
  34.  
  35.                 ;Check to see if the program has come to the function section near the end
  36.                 If hitfuncs = 1 Then
  37.                         For I=1 To 5
  38.                                 WriteLine(fileout,"")
  39.                                 Print ""
  40.                         Next
  41.  
  42.                         hitfuncs=2
  43.                 End If
  44.  
  45.  
  46.                 ;See if a blank space is necessary
  47.                 If needspace Then
  48.                         WriteLine(fileout,"")
  49.                         Print ""
  50.                         needspace=0
  51.                 End If
  52.  
  53.  
  54.                 ;Add the Tabs
  55.                 If newtab > 1 Then tab=tab-1 ;See if the tab needs to be taken down
  56.                 If tab< 0 Then tab=0
  57.                 I=0
  58.                 While I < tab
  59.                         linefeed=Chr$(9)+linefeed
  60.                         I=I+1
  61.                 Wend
  62.  
  63.                 If newtab Mod(2) Then tab=tab+1 ;See if the tab should be reversed or added to
  64.  
  65.                 ;Makes sure that there isn't a break for an Ending (forever, next, end function, etc.) command just Before another such command
  66.                 If newtab=2
  67.                         LastWasEnd=True
  68.                 Else
  69.                         If lastwasend Then
  70.                                 Print ""
  71.                                 WriteLine(fileout,"")
  72.                                 lastwasend=False
  73.                         End If
  74.                 End If
  75.  
  76.                 Print linefeed$
  77.                 WriteLine(fileout,linefeed) ;write the edited line
  78.         End If
  79. Wend
  80.  
  81.  
  82. ;I like to have plenty of space at the bottom of my code
  83. For I = 1 To 50
  84.         WriteLine(fileout,"")
  85. Next
  86.  
  87. Print "Finished"
  88. CloseFile(filein)
  89. CloseFile(fileout)
  90. Delay(10000)
  91.  
  92.  
  93.  
  94.  
  95.  
  96. Function ParseOutExt$(loc$);parses the name and location of a file w/o extension
  97.         Repeat
  98.                 If Not Instr(loc,".",placement+1) Then Exit
  99.                 placement=Instr(loc,".",placement+1)
  100.         Forever
  101.  
  102.         Return Left$(loc,placement-1)
  103. End Function
  104.  
  105.  
  106. ;parses the extension of a file location
  107. Function ParseExt$(loc$)
  108.         Repeat
  109.                 If Not Instr(loc,".",placement+1) Then Exit
  110.                 placement=Instr(loc,".",placement+1)
  111.         Forever
  112.  
  113.         Return Right$(loc,Len(loc)-placement+1)
  114. End Function
  115.  
  116. Function needstab%(LineIn$)
  117.         If linein <> ""
  118.  
  119.                 ;Prepare the input
  120.                 linein=Lower$(Trim$(linein))
  121.                 If linein="end" Then Return 0 ;Ensure there isn't a random "End" statement that messes with tabbing
  122.                 nostrings$=stripstrings$(linein)
  123.                 nocomments$=stripcomments$(linein)
  124.  
  125.                 ;parse out the first command of a line
  126.                 com$=";"+firstcom$(linein)+";"
  127.  
  128.                 ;check to see if the functions section of the program has begun
  129.                 If com$=";function;" And hitfuncs=0 Then hitfuncs=1
  130.  
  131.                 ;ensure that an "if" statement w/o an "end if" doesn't screw up the tabbing
  132.                 If Instr(nostrings,"then",0)
  133.                         If Right$(nostrings,4) <> "then" Or Instr(nostrings,":") Then Return 0
  134.                 End If
  135.  
  136.  
  137.                 ;find out how to classify the command
  138.                 If Instr(pluscoms,com$,0) ;add a tab
  139.                         Return 1
  140.                 ElseIf Instr(mincoms$,com$,0) ;delete a tab
  141.                         Return 2
  142.                 ElseIf Instr(backcoms,com$,0) ;temporarily delete a tab
  143.                         Return 3
  144.                 End If
  145.         End If
  146.  
  147.  
  148.         ;command does not require a tab change
  149.         Return 0
  150. End Function
  151.  
  152.  
  153. ;parses out the first word on a line
  154. Function firstcom$(linein$)
  155.         linein=Trim$(Replace$(linein,Chr$(9),""))
  156.         I=1
  157.         While (Not Instr(comends,Mid$(linein,I,1),0)) And (I <= Len(linein$))
  158.                 I=I+1
  159.         Wend
  160.  
  161.         Return Lower$(Trim$(Left$(linein,I-1)))
  162. End Function
  163.  
  164.  
  165. ;strips away comments
  166. Function StripComments$(linein$)
  167.         I=Instr(linein,";")
  168.         Return Left$(Trim$(linein),I-1)
  169. End Function
  170.  
  171.  
  172. ;strips away strings
  173. Function stripstrings$(linein$)
  174.         While Instr(linein,Chr$(34))
  175.                 placement=Instr(linein,Chr$(34))
  176.                 If placement
  177.                         endquote=Instr(linein,Chr$(34),placement+1)
  178.                         linein=Left$(linein,placement-1)+Right$(linein,Len(linein)-endquote)
  179.                 End If
  180.         Wend
  181.  
  182.         Return linein
  183. End Function
  184.  
  185.  
  186. ;used for identifying declaration functions (global, const, local, etc.)
  187. Function cleanheader(linein$)
  188.         Local com$=";"+firstcom$(linein)+";"
  189.         Select 1
  190.         Case Instr(Globalcoms,com$,0)
  191.                 If lastcomset=0 Then lastcomset=1
  192.                 If lastcomset <> 1 Then WriteLine(fileout,"")
  193.                 lastcomset=1
  194.         Case Instr(constcoms,com$,0)
  195.                 If lastcomset=0 Then lastcomset=2
  196.                 If lastcomset <> 2 Then WriteLine(fileout,"")
  197.                 lastcomset=2
  198.         Case Instr(arraycoms,com$,0)
  199.                 If lastcomset=0 Then lastcomset=3
  200.                 If lastcomset <> 3 Then WriteLine(fileout,"")
  201.                 lastcomset=3
  202.         Case Instr(localcoms,com$,0)
  203.                 If lastcomset=0 Then lastcomset=4
  204.                 If lastcomset <> 4 Then WriteLine(fileout,"")
  205.                 lastcomset=4
  206.         Case Instr(headercoms,com$,0)
  207.                 If lastcomset=0 Then lastcomset=5
  208.                 If lastcomset <> 5 Then WriteLine(fileout,"")
  209.                 lastcomset=5
  210.         Default
  211.                 lastcomset=0
  212.         End Select
  213. End Function


Comments :


Rook Zimbabwe(Posted 1+ years ago)

 so is this for B3d or BMax or ???


Damien Sturdy(Posted 1+ years ago)

 <div class="quote"> ;Average Commands </div>looks like BlitzBasic, to me.


Alaric(Posted 1+ years ago)

 actually its b+ but might work in b3d as well


Ryudin(Posted 1+ years ago)

 Thanks for posting! I feel the same way as you (including the empty lines at the bottom).


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal