32

Congratulations, what you have here is a case of really really bad backtracking. Remember the StackOverflow outage in July 2016? Because it's explained quite nicely there. regex101 is so kind as to tell us in the debugger view how many steps the regex takes until it matches. In case you were wondering, it's 111,803 We can drastically reduce the number of ...


16

Answers to your specific questions: My first question is, have I made any common C errors, like buffer overflows or unreleased memory? A leak is always connected to a resource. A resource is by definition something that you acquire manually, and that you must release manually. Memory is a prime example, but there are other resources, too (file handles, ...


15

Overall, this looks like pretty good JavaScript code. I would have two comments though, to avoid errors, long term (i.e. maintainability): Direct Statement if(expr) statement; This is correct in all C-like languages, unfortunately. Only, if you write two statements after the if() and forget to add the curly brackets, you end up with a bug. I always write ...


13

Packaging Compiling statically is tricky. I understand your reasoning, but if you want e.g. to get your program into a distribution, having a proper build with a separate package for each library is preferable (even necessary?). If you provide a compiled distribution yourself, your users won't object I guess. For dependencies, I usually expect no bundled ...


8

A few things to point out: ul: function(data) { var lis = childnodes[data.i].childNodes; var newmd = ''; var lislength = lis.length; for (var x = 0; x < lislength; x++) { newmd += "- " + lis[x].innerHTML + "\n"; } return newmd; }, You can declare lislength in the for loop, rather than externally, if you like. for (var ...


8

Here are a few suggestions: String comparisons should be done with == (and !=) instead of is (and is not). is works, but implies you are comparing identity (which is usually a memory address), whereas == is comparing value. See https://stackoverflow.com/a/2988117/331473 for more information. Your boolean configs (eg: minify_html) should be actual boolean ...


7

I prefer emphasize constants in UPPERCASE. Imports should usually be on separate lines, e.g.: Yes: import os import sys No: import sys, os Use spaces instead of tabs. Avoid to name variables as built-in functions/objects. I mean file. If you need this function later, it will take time to figure out why it doesn't work. With keyword closes file ...


7

OnPropertyChanged() This method to raise the PropertyChanged event needs only to be called if the value is changed which isn't verified by the setters of your properties yet. To fix this issue and prevent unneeded work to be done a simple if condition is needed like so public string MarkdownContent { get { return _markdownContent; } set { ...


7

I have just signed up, so I can only post answers and not comment on the otherwise nice answer of Vogel612. I would like to add that it is also possible in PCRE to make {3,} not backtrack at all. You can do this by adding a + behind it. In this case, it is completely equivalent, because the part before and after the added + don't overlap (except for an ...


7

<RichTextBox x:Class="MarkdownViewer.MarkdownBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" ...


6

You want to unleash the almighty power of WPF, and bind to a ViewModel; WPF and the Model-View-ViewModel design go completely hand-in-hand, especially if you like your code testable. As it stands the only way to test the application logic is to actually run it and see what happens - that's good for a prototype app, but for an actual real-world app you'll ...


6

While your code looks ok, it could be improved greatly by making use of RS (record separator) and NR number of record, provided you're using GNU awk. awk -v RS='```[a-z]*\n' '(NR+1)%2' file In this case RS is set such that it is catching everything between triple backticks with optional text. The only awk statement is to print one record out of two.


5

Not sure where to start.. First off, you might want to look at this response as to whether using regexes for parsing markdown to HTML is safe. You are of course not parsing HTML, but the sentiment is still valid in my mind. Second off, there is this and this on github, these parsers are a few hundred lines. You are missing use cases in your 30 lines of ...


5

I like the other two answers so far and I see you've already added some of the advice in. I have two small additions: this might be shorter but it is harder to read: for nonfile in nonentryfiles: textfiles.remove(config["directory"]+"/"+nonfile) # Except for non-entry files I'm guessing you've come from a language that emphasizes being consise but ...


5

OrderByVersion You've taken the time to add doc comments, but a vital piece of info is missing. The enumerable is sorted in descending order. That should be there in the doc comments. It would be nice to have an option to sort ascending. (It's also an interesting problem to solve. =;)- The mixture of query and lambda syntax seems a bit strange to me. I'd ...


4

/// <summary> /// Extract SemVer format from string into an array /// </summary> /// <param name="version">A version in the SemVer format</param> /// <returns>An array of string composed by SemVer format</returns> /// <example> /// v0 will give [0, "", ""] /// v1.2.3-foobar will give [1, 2, 3] /// </example> ...


4

Your code isn't that much re-usable. I can't include it in a file and call it since I could easily break it from anywhere. What I suggest is that you wrap it in the following block: (function(window, undefined){ 'use strict'; [code] })(Function('return this')()); This will protect your code in so many ways: You guarantee that the window object is ...


4

Some feedback on the XAML: Good that you set d:DataContext as it saves some pain when bindings are ~compiletime checked (if the designer has the view open) <RowDefinition Height="*"/> *is the default but I don't mind being explicit about it. <DockPanel Grid.ColumnSpan="3"> usually you want to avoid ColumnSpan. Nesting panels simplifies layout ...


4

The Code looks perfect to me. I thought about using the flip-flop operator, but since you take additional action at the beginning and the end of the code block, this may be difficult in this case. /^```/, /^```/ { ... } Maybe you want to experiment with that idea nevertheless. It may prove valuable in the future.


4

Your program expects the user to refer to completed or incomplete tasks by a numeric index. However, the Markdown file contains an unnumbered bulleted list, which forces the user to count the items manually (starting from 0). That design is inhumane. I suggest that you change the Markdown to use numbered lists. By the way, "uncompleted" is not common in ...


3

First change - extract iteration logic to its own generator: def markdown_files(config): sources_dir = config['sources_dir'] for root, dirnames, filenames in os.walk(sources_dir): for filename in fnmatch.filter(filenames, '*.md'): yield os.path.join(root, filename) def main(): config = get_user_config(os.getcwd()) for ...


3

One way is to use multiplication. Multiplying a String by a Fixnum will produce a new String where the original String appears the Fixnum of times. "-" * @title.length #=> "---------------" Documentation for String#* Warning: Make sure you put the String first or else you will get a TypeError: String can't be coerced into Fixnum since that would be ...


3

I'd strongly suggest moving the HTML content out of the code and into separate files; I know you want to keep this simple and single-file but in this case there some real benefits to treating the HTML as a resource rather than as code: HTML is a finicky format. It's easy to screw up an escape or lose a bracket and mess up the output. With separate files it'...


3

Don't do this: #!/usr/bin/env python Some people make the poor excuse that PEP 394 recommends that python continue to refer to python2 for the time being but this doesn't even apply to you since yours only supports Python 3! Use this instead: #!/usr/bin/env python3 Your code could do with doc comments; at least load_html_doc could. These don't need ...


3

A bug If your html contains a backspace character e.g <p>The special character \n is a way to include read line</p> your code interprets it a The special character n is a way to include read line. to avoid this, add "-r" switch to your read line cat hot-form-validator.html | while read -r "line";do You want to check if your file exists ...


3

Now I may be wrong, but I am able to match: ------- from 3 and up - - - - - from 3 and up While not matching the 'abc' example (or any line that throws a non-horizontal rule symbol into the mix anywhere in the line), with: /^ *[\-\*_] *[\-\*_] *[\-\*_][\-\*_ \t]*$/ in 66 steps. My one concern is that I'm not sure if you are wanting to allow multiple ...


3

I have a similar script. So from my experiences I would: build the pdfs through latexmk sometimes it needs more runs of pdflatex. I found it too hard to get right in plain make. have directories for work and artefacts. I don't like to have temporary files among my carefully written texts. The risk of an accidental rm or overwrite is just always present....


3

I may not be considering all fringe cases (please enlighten me if this breaks with any realistic input), but all of the logic can be packed into a single preg_replace_callback() call. I start by declaring a 1-dimensional array which will contain all numeric counters. My pattern ~^(#+)\K~m says: From the start of each line, capture one or more hash symbols ...


2

The whole approach of using regexes to parse Markdown and convert it into HTML is broken. You have no way of knowing whether any html you generate with one call to gsub is violating the constraints of the HTML containing it. After the first gsub call, you iteratively apply it to text which now contains some html. You may mistakenly replace parts of an html ...


2

You could make your code more reusable (for example, from another Python program) by making it into a function. The descriptive comment would then naturally be the docstring of this function, which allows it to be accessed by other tools, like the help function in an interactive Python session, or the pydoc command-line tool. To allow both using the ...


Only top voted, non community-wiki answers of a minimum length are eligible